C# Indexer : For storing flags

C# Indexer : For storing flags

am 04.04.2008 21:50:01 von Eric

Will someone tell me if this is an efficient and appropriate way of storing
strongly typed class properties? I want a cleaner approach to "property per
flag" method and I want things strongly typed for design time if possible.


class Program
{
static void Main(string[] args)
{
MyClass myClass = new MyClass();

Console.WriteLine(myClass[MyClass.Property.Property1]);
Console.WriteLine(myClass[MyClass.Property.Property2]);
Console.WriteLine(myClass[MyClass.Property.Property3]);
Console.ReadLine();
}
}

public class MyClass
{

public enum Property
{
Property1,
Property2,
Property3
}

private Dictionary _propertyData = new
Dictionary();

public MyClass()
{
_propertyData[Property.Property1] = true;
_propertyData[Property.Property2] = false;
_propertyData[Property.Property3] = true;
}

public bool this[Property p]
{
get { return _propertyData[p]; }
}
}


Thank You

Eric Brasher

Re: C# Indexer : For storing flags

am 04.04.2008 22:30:05 von Anthony Jones

"Eric" wrote in message
news:235F61B0-37F9-47B6-A998-22BB2C718BBE@microsoft.com...
> Will someone tell me if this is an efficient and appropriate way of
storing
> strongly typed class properties? I want a cleaner approach to "property
per
> flag" method and I want things strongly typed for design time if possible.
>
>
> class Program
> {
> static void Main(string[] args)
> {
> MyClass myClass = new MyClass();
>
> Console.WriteLine(myClass[MyClass.Property.Property1]);
> Console.WriteLine(myClass[MyClass.Property.Property2]);
> Console.WriteLine(myClass[MyClass.Property.Property3]);
> Console.ReadLine();
> }
> }
>
> public class MyClass
> {
>
> public enum Property
> {
> Property1,
> Property2,
> Property3
> }
>
> private Dictionary _propertyData = new
> Dictionary();
>
> public MyClass()
> {
> _propertyData[Property.Property1] = true;
> _propertyData[Property.Property2] = false;
> _propertyData[Property.Property3] = true;
> }
>
> public bool this[Property p]
> {
> get { return _propertyData[p]; }
> }
> }
>
>

It looks like it would work but its seem a strange thing to want to do.

Why won't this do:-

//C# 2
public class MyClass
{
bool _property1;
bool _property2;
bool _property3;

public MyClass()
{
_property1 = true;
_property2 = false;
_property3 = true;
}

public bool Property1 { get { return _property1; } }
public bool Property2 { get { return _property2; } }
public bool Property3 { get { return _property3; } }
}

OR

//C# 3
public class MyClass
{

public MyClass()
{
Property1 = true;
Property2 = false;
Property3 = true;
}

public bool Property1 { get; private set; }
public bool Property2 { get; private set; }
public bool Property3 { get; private set; }
}



Perhaps what you need is:-


public class MyClass
{
[Flags]
public Enum MyClassProperties : Int32
{
Property1 = 1;
Property2 = 2;
Property3 = 4;
}

MyClassProperties myClassProperties;

public MyClass()
{
myClassProperties = MyClassProperties.Property1 |
MyClassProperties.Property3;
}

public bool this[MyClassProperties index]
{
get { return (myClassProperties & index) != 0); }
}
}


This limits you to 32 properties but the concept can be taken further.


--
Anthony Jones - MVP ASP/ASP.NET