Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. create enum at run time

create enum at run time

Scheduled Pinned Locked Moved C#
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    a hamidy
    wrote on last edited by
    #1

    Dear All, I have a DataTable which has 8 records and it will have more records at later too. the data table has two fields (columns) which is "Id" and "Name". I would like to create enum based on "Name" field and the value of enum based on "Id" field. Is it possible to create enums at run time using c#.

    M M P 3 Replies Last reply
    0
    • A a hamidy

      Dear All, I have a DataTable which has 8 records and it will have more records at later too. the data table has two fields (columns) which is "Id" and "Name". I would like to create enum based on "Name" field and the value of enum based on "Id" field. Is it possible to create enums at run time using c#.

      M Offline
      M Offline
      musefan
      wrote on last edited by
      #2

      Well how exactly are you hoping to use them?

      Life goes very fast. Tomorrow, today is already yesterday.

      A 1 Reply Last reply
      0
      • A a hamidy

        Dear All, I have a DataTable which has 8 records and it will have more records at later too. the data table has two fields (columns) which is "Id" and "Name". I would like to create enum based on "Name" field and the value of enum based on "Id" field. Is it possible to create enums at run time using c#.

        M Offline
        M Offline
        Mustafa Ismail Mustafa
        wrote on last edited by
        #3

        You can't. The enum has to be created before hand, but you can save the values to the database and parse them at run time by using Enum.Parse(). Please note that you ought to use "Enum" and not "enum", the first is a type the second is a keyword

        If the post was helpful, please vote!


        Why won't the worm just leave me be?

        A 1 Reply Last reply
        0
        • M musefan

          Well how exactly are you hoping to use them?

          Life goes very fast. Tomorrow, today is already yesterday.

          A Offline
          A Offline
          a hamidy
          wrote on last edited by
          #4

          well i have checkboxes in many forms. when they are checked i want to insert the enum value in the child table. i can assign the value without run time, but i am afraid if the table "id" is changed than i am stack, so i am creating enums at run time.

          M 1 Reply Last reply
          0
          • M Mustafa Ismail Mustafa

            You can't. The enum has to be created before hand, but you can save the values to the database and parse them at run time by using Enum.Parse(). Please note that you ought to use "Enum" and not "enum", the first is a type the second is a keyword

            If the post was helpful, please vote!


            Why won't the worm just leave me be?

            A Offline
            A Offline
            a hamidy
            wrote on last edited by
            #5

            I have gone to create enum at run time using System.Reflection.Emit.EnumBuilder. i can create enums at run time using mentioned class. and hopefully it can be destroyed when you close the application.

            M 1 Reply Last reply
            0
            • A a hamidy

              well i have checkboxes in many forms. when they are checked i want to insert the enum value in the child table. i can assign the value without run time, but i am afraid if the table "id" is changed than i am stack, so i am creating enums at run time.

              M Offline
              M Offline
              musefan
              wrote on last edited by
              #6

              sorry you are going to have to try to explain that again. why dont you give an example of a few check boxes and the values you would like to insert into your table when they are checked/unchecked

              Member 6059028 wrote:

              i am afraid if the table "id" is changed than i am stack

              Do you mean the column name of the table or do you mean the actually id value for the table entry?

              Life goes very fast. Tomorrow, today is already yesterday.

              1 Reply Last reply
              0
              • A a hamidy

                I have gone to create enum at run time using System.Reflection.Emit.EnumBuilder. i can create enums at run time using mentioned class. and hopefully it can be destroyed when you close the application.

                M Offline
                M Offline
                Mustafa Ismail Mustafa
                wrote on last edited by
                #7

                Reflection is another matter. How are you expecting to be able to utilize it then?

                If the post was helpful, please vote!


                Why won't the worm just leave me be?

                1 Reply Last reply
                0
                • A a hamidy

                  Dear All, I have a DataTable which has 8 records and it will have more records at later too. the data table has two fields (columns) which is "Id" and "Name". I would like to create enum based on "Name" field and the value of enum based on "Id" field. Is it possible to create enums at run time using c#.

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  You can, but I don't see a reason to. I'd suggest using a Dictionary or something. Or this[^].

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups