Click here to Skip to main content
15,314,644 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

i am not sure if this is at all possible, and i have tried myself but cannot seem to get anywhere with it:

i have several sub classes that perform a bunch of sql statements and hold values in sub classes, one downside to this is that i have to have to setup properties under each class to hold the values.

My thoughts where to create a class that can be inherited or something to the parent class that i could pass an enum (enum contains all the field names and ordinal), the child class would then create a dictionary of that enumeration and string to hold the values.

By creating the dictionary as such i can use one generic class to hold all the parameters without having to write out the properties and the class in each class that i have, just trying to avoid repeated code all over the place (pet hate)

why write something 20 times when you can write an adaptable class to serve your needs! :)

So, just to clarify. I am looking to pass a bunch of different enums to the same class instantiation method and then convert this to a dictionary of the passed enum and string but due to different enums being passed it has to be done on the fly if that makes sense? So once the dictionary is populated, and i use Dictionary[] it then brings me the list of enum members?

As it statnds the 'EnumObj'
C#
this.objDict = new Dictionary<EnumObj, String>();


highlights witht his error message
The type or namespace name 'EnumObj' could not be found (are you missing a using directive or an assembly reference?)


Here is the base example code i am working with
C#
class ParentClass
{
    /// <summary>
    /// Holds the properties
    /// </summary>
    public ChildClass Properties;

    public ParentClass()
    {
        this.Properties = new ChildClass(typeof(MyEnum));
    }

    enum MyEnum
    {
        col1 = 1,
        col2 = 2,
        col3 = 3
    }



}
/// <summary>
/// Holds the properties values for the array record
/// </summary>
public class ChildClass
{

    private Object objDict;

    public ChildClass(Type EnumObj)
    {
        this.objDict = new Dictionary<EnumObj, String>();
    }
    public ChildClass(Object EnumObj)
    {
        this.objDict = new Dictionary<typeof(EnumObj), String>();
    }
}


What I have tried:

Different Variations on passing the method, i have trying passing the enum as an object rather than a type and then convert it.
Posted
Updated 24-Aug-16 1:03am

1 solution

Since you're using generic dictionary the child class should be generic.
Try this:
C#
/// <summary>
/// Holds the properties values for the array record
/// </summary>
public class ChildClass<TEnum>
{
    private Dictionary<TEnum,String> objDict;
 
    public ChildClass()
    {
        this.objDict = new Dictionary<TEnum,String>();
    }
}

class ParentClass
{
    /// <summary>
    /// Holds the properties
    /// </summary>
    public ChildClass<MyEnum> Properties;
 
    public ParentClass()
    {
        this.Properties = new ChildClass<MyEnum>();
    }
 
    enum MyEnum
    {
        col1 = 1,
        col2 = 2,
        col3 = 3
    } 
}
   
v3
Comments
Dev O'Connor 24-Aug-16 7:13am
   
Spot on, exactly what i wanted to achieve. Really apprecaite the help 5+ for me :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900