C# Indexer : For storing flags

E

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<Property, bool> _propertyData = new
Dictionary<Property,bool>();

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
 
A

Anthony Jones

Eric said:
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<Property, bool> _propertyData = new
Dictionary<Property,bool>();

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top