Load Data from Database as Class Properties or enum

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Waqas Pitafi

    #1

    Load Data from Database as Class Properties or enum

    Hi,

    I have a table with the data like this,

    ID Status Description
    1 R Read
    2 U UnRead
    D D Deleted

    I want to load this data in a class (say named Status) which I want to use
    through out my project and should be able to access it like,

    Status.Read or
    Status.UnRead or
    Status.Deleted

    with each returning their corresponding values as in the database table
    shown above.

    To sum it up I want to create an enum with values loaded from database. I
    think it may be possible using Reflection or Custom Attributes but don't know
    how to do it.

    One more thing is if I use reflection than I guess I won't be able to use
    Status.Read in design time which means no use of it, correct me if I am wrong.

    Any ideas how to go about it?

    Regards,
    Waqas Pitafi
  • Miha Markic [MVP C#]

    #2
    Re: Load Data from Database as Class Properties or enum

    Hi Waqas,

    Why would you want to create an enum at runtime?
    Why don't you simply use DataTable?
    And yes, if you create enum at runtime there is no way of using it at design
    time.

    --
    Miha Markic [MVP C#] - RightHand .NET consulting & development
    miha at rthand com


    "Waqas Pitafi" <Waqas Pitafi@discussi ons.microsoft.c om> wrote in message
    news:BC7D1BB3-49EB-4C86-B475-2DEE7D99C536@mi crosoft.com...[color=blue]
    > Hi,
    >
    > I have a table with the data like this,
    >
    > ID Status Description
    > 1 R Read
    > 2 U UnRead
    > D D Deleted
    >
    > I want to load this data in a class (say named Status) which I want to use
    > through out my project and should be able to access it like,
    >
    > Status.Read or
    > Status.UnRead or
    > Status.Deleted
    >
    > with each returning their corresponding values as in the database table
    > shown above.
    >
    > To sum it up I want to create an enum with values loaded from database. I
    > think it may be possible using Reflection or Custom Attributes but don't
    > know
    > how to do it.
    >
    > One more thing is if I use reflection than I guess I won't be able to use
    > Status.Read in design time which means no use of it, correct me if I am
    > wrong.
    >
    > Any ideas how to go about it?
    >
    > Regards,
    > Waqas Pitafi[/color]


    Comment

    • Waqas Pitafi

      #3
      Re: Load Data from Database as Class Properties or enum

      Thanks for your quick reply.

      I have a master table which is holding Status values. Based on these values
      I have to perform different jobs in different classes.

      e.g. Status.Read would mean to show only Read records on various controls.

      Right now I have created a duplicate enum class representing my Database
      Table precisely but latter if there is a new Status added or any current
      record edited I will have to make adjustments to the code and recompile
      application. Currently I have created an enum like this,

      public enum QuestionsStatus
      {
      Ignore = -1,
      Unread = 1,
      Read = 2,
      ForwardedToCons ultant = 3,
      ForwardedToMe = 4,
      Answered = 5,
      MarkedForDeleti on = 6
      }
      This is exact representation of the data in the database table.

      I hope you got it now why I need this. I don't want to hard code these
      status values in my code so if 1 is edited I have to go and change it at
      every place it's used.

      "Miha Markic [MVP C#]" wrote:
      [color=blue]
      > Hi Waqas,
      >
      > Why would you want to create an enum at runtime?
      > Why don't you simply use DataTable?
      > And yes, if you create enum at runtime there is no way of using it at design
      > time.
      >
      > --
      > Miha Markic [MVP C#] - RightHand .NET consulting & development
      > miha at rthand com
      > www.rthand.com
      >
      > "Waqas Pitafi" <Waqas Pitafi@discussi ons.microsoft.c om> wrote in message
      > news:BC7D1BB3-49EB-4C86-B475-2DEE7D99C536@mi crosoft.com...[color=green]
      > > Hi,
      > >
      > > I have a table with the data like this,
      > >
      > > ID Status Description
      > > 1 R Read
      > > 2 U UnRead
      > > D D Deleted
      > >
      > > I want to load this data in a class (say named Status) which I want to use
      > > through out my project and should be able to access it like,
      > >
      > > Status.Read or
      > > Status.UnRead or
      > > Status.Deleted
      > >
      > > with each returning their corresponding values as in the database table
      > > shown above.
      > >
      > > To sum it up I want to create an enum with values loaded from database. I
      > > think it may be possible using Reflection or Custom Attributes but don't
      > > know
      > > how to do it.
      > >
      > > One more thing is if I use reflection than I guess I won't be able to use
      > > Status.Read in design time which means no use of it, correct me if I am
      > > wrong.
      > >
      > > Any ideas how to go about it?
      > >
      > > Regards,
      > > Waqas Pitafi[/color]
      >
      >
      >[/color]

      Comment

      • Miha Markic [MVP C#]

        #4
        Re: Load Data from Database as Class Properties or enum

        CodeSmtih is a perfect tool for what are you after.


        --
        Miha Markic [MVP C#] - RightHand .NET consulting & development
        miha at rthand com


        "Waqas Pitafi" <WaqasPitafi@di scussions.micro soft.com> wrote in message
        news:BBC9756F-9194-409D-B0ED-73AC664FE7E7@mi crosoft.com...[color=blue]
        > Thanks for your quick reply.
        >
        > I have a master table which is holding Status values. Based on these
        > values
        > I have to perform different jobs in different classes.
        >
        > e.g. Status.Read would mean to show only Read records on various controls.
        >
        > Right now I have created a duplicate enum class representing my Database
        > Table precisely but latter if there is a new Status added or any current
        > record edited I will have to make adjustments to the code and recompile
        > application. Currently I have created an enum like this,
        >
        > public enum QuestionsStatus
        > {
        > Ignore = -1,
        > Unread = 1,
        > Read = 2,
        > ForwardedToCons ultant = 3,
        > ForwardedToMe = 4,
        > Answered = 5,
        > MarkedForDeleti on = 6
        > }
        > This is exact representation of the data in the database table.
        >
        > I hope you got it now why I need this. I don't want to hard code these
        > status values in my code so if 1 is edited I have to go and change it at
        > every place it's used.
        >
        > "Miha Markic [MVP C#]" wrote:
        >[color=green]
        >> Hi Waqas,
        >>
        >> Why would you want to create an enum at runtime?
        >> Why don't you simply use DataTable?
        >> And yes, if you create enum at runtime there is no way of using it at
        >> design
        >> time.
        >>
        >> --
        >> Miha Markic [MVP C#] - RightHand .NET consulting & development
        >> miha at rthand com
        >> www.rthand.com
        >>
        >> "Waqas Pitafi" <Waqas Pitafi@discussi ons.microsoft.c om> wrote in message
        >> news:BC7D1BB3-49EB-4C86-B475-2DEE7D99C536@mi crosoft.com...[color=darkred]
        >> > Hi,
        >> >
        >> > I have a table with the data like this,
        >> >
        >> > ID Status Description
        >> > 1 R Read
        >> > 2 U UnRead
        >> > D D Deleted
        >> >
        >> > I want to load this data in a class (say named Status) which I want to
        >> > use
        >> > through out my project and should be able to access it like,
        >> >
        >> > Status.Read or
        >> > Status.UnRead or
        >> > Status.Deleted
        >> >
        >> > with each returning their corresponding values as in the database table
        >> > shown above.
        >> >
        >> > To sum it up I want to create an enum with values loaded from database.
        >> > I
        >> > think it may be possible using Reflection or Custom Attributes but
        >> > don't
        >> > know
        >> > how to do it.
        >> >
        >> > One more thing is if I use reflection than I guess I won't be able to
        >> > use
        >> > Status.Read in design time which means no use of it, correct me if I am
        >> > wrong.
        >> >
        >> > Any ideas how to go about it?
        >> >
        >> > Regards,
        >> > Waqas Pitafi[/color]
        >>
        >>
        >>[/color][/color]


        Comment

        Working...