how to parse an Enum Structure in vb.net

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

    how to parse an Enum Structure in vb.net

    I have an Enum Structure

    Public Enum MyEnum
    EnumVal1=0
    EnumVal2=1
    EnumVal2=2

    end enum

    I save in an access database this enum value as an integer (0=EnumVal1,
    1=EnumVal2, 2=EnumVal)


    When retreving this enum from the database how do I ensure that the correct
    value is passed to my object

    For instance


    MyObject.MyEnum =1 .
    Does this populate the property with MyObject.MyEnum = EnumVal2
    giannik


  • Herfried K. Wagner [MVP]

    #2
    Re: how to parse an Enum Structure in vb.net

    "giannik" <giannik@newsgr oups.nospamschr ieb:
    >I have an Enum Structure
    >
    Public Enum MyEnum
    EnumVal1=0
    EnumVal2=1
    EnumVal2=2
    >
    end enum
    >
    I save in an access database this enum value as an integer (0=EnumVal1,
    1=EnumVal2, 2=EnumVal)
    \\\
    Dim e As MyEnum = CType(v, MyEnum)
    ///

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://classicvb.org/petition/>

    Comment

    • Patrice

      #3
      Re: how to parse an Enum Structure in vb.net

      You have nothing special to do. See EnumVal2 has being just a symbolic name
      for the value 1 (similarly to a constant).

      So if MyObject.MyEnum =1, MyObject.MyEnum =MyEnum.EnumVal 2 because
      MyEnum.EnumVal2 is also 1.

      --
      Patrice

      "giannik" <giannik@newsgr oups.nospama écrit dans le message de news:
      %23WnH1%23CoGHA .4616@TK2MSFTNG P05.phx.gbl...
      >I have an Enum Structure
      >
      Public Enum MyEnum
      EnumVal1=0
      EnumVal2=1
      EnumVal2=2
      >
      end enum
      >
      I save in an access database this enum value as an integer (0=EnumVal1,
      1=EnumVal2, 2=EnumVal)
      >
      >
      When retreving this enum from the database how do I ensure that the
      correct value is passed to my object
      >
      For instance
      >
      >
      MyObject.MyEnum =1 .
      Does this populate the property with MyObject.MyEnum = EnumVal2
      giannik
      >

      Comment

      • giannik

        #4
        Re: how to parse an Enum Structure in vb.net

        ok. Thank you very much

        "Patrice" <scribe@chez.co mwrote in message
        news:e8NireDoGH A.4648@TK2MSFTN GP05.phx.gbl...
        You have nothing special to do. See EnumVal2 has being just a symbolic
        name for the value 1 (similarly to a constant).
        >
        So if MyObject.MyEnum =1, MyObject.MyEnum =MyEnum.EnumVal 2 because
        MyEnum.EnumVal2 is also 1.
        >
        --
        Patrice
        >
        "giannik" <giannik@newsgr oups.nospama écrit dans le message de news:
        %23WnH1%23CoGHA .4616@TK2MSFTNG P05.phx.gbl...
        >>I have an Enum Structure
        >>
        >Public Enum MyEnum
        > EnumVal1=0
        > EnumVal2=1
        > EnumVal2=2
        >>
        >end enum
        >>
        >I save in an access database this enum value as an integer (0=EnumVal1,
        >1=EnumVal2, 2=EnumVal)
        >>
        >>
        >When retreving this enum from the database how do I ensure that the
        >correct value is passed to my object
        >>
        >For instance
        >>
        >>
        >MyObject.MyEnu m=1 .
        >Does this populate the property with MyObject.MyEnum = EnumVal2
        >giannik
        >>
        >
        >

        Comment

        • Phill W.

          #5
          Re: how to parse an Enum Structure in vb.net

          giannik wrote:
          I have an Enum
          >
          Public Enum MyEnum
          EnumVal1=0
          EnumVal2=1
          EnumVal2=2
          end enum
          >
          I save in an access database this enum value as an integer (0=EnumVal1,
          1=EnumVal2, 2=EnumVal)
          Save the Enum /names/ not their numeric values.
          OK, you only have three here, but what if you had a long list of these
          and then, Lord forbid, you added another one "in the middle"? Much of
          your existing data (in Access) would be wrong.

          Use a String column in the database and use <value>.ToStrin g() to get
          the enum "name" to save.
          When retreving this enum from the database how do I ensure that the correct
          value is passed to my object
          When reading the property back, parse the string value (from Access)
          back into the Enum Type, as in

          MyObject.MyEnum = CType( dr.Item( "EV" ), MyEnum )

          By way of a rather silly example:

          Class MyClass
          Enum Month
          January = 1
          February
          March
          . . .

          dr.Item( "Month" ) = Month.December. ToString() ' actually "December"

          Now, just for the sake of argument, let's create a new month, called
          Filibuster, between February and March.

          Class MyClass
          Enum Month
          January = 1
          February
          Filibuster
          March
          . . .

          Oh No! I hear you cry. You'll have to bulk update all the records in
          Access to increment their month numbers!
          Nope. Holding the Enum /names/ means you don't have to. Assuming you
          already have a row in there for December:

          ? dr.Item( "Month" ).GetType().ToS tring()
          [System.]String
          ? dr.Item( "Month" ).ToString()
          "December"
          ? CType( dr.Item( "Month" ), Month ).GetType().ToS tring()
          [MyClass.]Month
          ? CType( dr.Item( "Month" ), Month )
          December

          So far, so good, but here's the clincher ...

          ? CInt( dr.Item( "Month" ), Month )
          13

          .... even if it was 12 when you saved that record into Access!

          HTH,
          Phill W.

          Comment

          • Dennis

            #6
            Re: how to parse an Enum Structure in vb.net

            It seems a waste to use strings as opposed to integers in a database for
            ENUMS. You can just as easily assign numbers to each enum value, i.e.,

            Enum Month
            January = 10
            February = 20
            March = 30
            . . .

            In the new enum;
            Enum Month
            January = 10
            February =20
            Filibuster = 25
            March =30
            . . .

            --
            Dennis in Houston


            "Phill W." wrote:
            giannik wrote:
            I have an Enum

            Public Enum MyEnum
            EnumVal1=0
            EnumVal2=1
            EnumVal2=2
            end enum

            I save in an access database this enum value as an integer (0=EnumVal1,
            1=EnumVal2, 2=EnumVal)
            >
            Save the Enum /names/ not their numeric values.
            OK, you only have three here, but what if you had a long list of these
            and then, Lord forbid, you added another one "in the middle"? Much of
            your existing data (in Access) would be wrong.
            >
            Use a String column in the database and use <value>.ToStrin g() to get
            the enum "name" to save.
            >
            When retreving this enum from the database how do I ensure that the correct
            value is passed to my object
            >
            When reading the property back, parse the string value (from Access)
            back into the Enum Type, as in
            >
            MyObject.MyEnum = CType( dr.Item( "EV" ), MyEnum )
            >
            By way of a rather silly example:
            >
            Class MyClass
            Enum Month
            January = 1
            February
            March
            . . .
            >
            dr.Item( "Month" ) = Month.December. ToString() ' actually "December"
            >
            Now, just for the sake of argument, let's create a new month, called
            Filibuster, between February and March.
            >
            Class MyClass
            Enum Month
            January = 1
            February
            Filibuster
            March
            . . .
            >
            Oh No! I hear you cry. You'll have to bulk update all the records in
            Access to increment their month numbers!
            Nope. Holding the Enum /names/ means you don't have to. Assuming you
            already have a row in there for December:
            >
            ? dr.Item( "Month" ).GetType().ToS tring()
            [System.]String
            ? dr.Item( "Month" ).ToString()
            "December"
            ? CType( dr.Item( "Month" ), Month ).GetType().ToS tring()
            [MyClass.]Month
            ? CType( dr.Item( "Month" ), Month )
            December
            >
            So far, so good, but here's the clincher ...
            >
            ? CInt( dr.Item( "Month" ), Month )
            13
            >
            .... even if it was 12 when you saved that record into Access!
            >
            HTH,
            Phill W.
            >

            Comment

            • Göran Andersson

              #7
              Re: how to parse an Enum Structure in vb.net

              Storing the integer value rather than the name does perform better.
              Storing the names is something that should be considered in some cases,
              though.

              In some cases the names correspond naturally to a number that won't
              change, for an example the months that are numbered from 1 through 12.
              Then there is no reason to store the name instead of the number.

              In other cases the names have no natural numbering at all, then it might
              be better to store the names. The numbers are only used internally by
              the application, and the data in the database is meningful without a
              translation table.

              Dennis wrote:
              It seems a waste to use strings as opposed to integers in a database for
              ENUMS. You can just as easily assign numbers to each enum value, i.e.,
              >
              Enum Month
              January = 10
              February = 20
              March = 30
              . . .
              >
              In the new enum;
              Enum Month
              January = 10
              February =20
              Filibuster = 25
              March =30
              . . .
              >

              Comment

              Working...