Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / July 2007

Tip: Looking for answers? Try searching our database.

enum problems

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Perecli Manole - 02 Jul 2007 22:51 GMT
I need to store an enum datatype in a variable and then reference it members
from the variable. I have tried this but it does not work.

   Private Enum enuTest1 As Byte
       Opt1 = 0
       Opt2 = 1
       Opt3 = 2
   End Enum

   Private Enum enuTest2 As Byte
       Opt1 = 5
       Opt2 = 6
       Opt3 = 7
   End Enum

   Private Sub Test()

       Dim a As Type = GetType(enuTest1)
       Dim b As Byte = a.Opt2

   End Sub

I need b to get the value of 1 since Opt2 in enuTest1 is 1.
However if I put GetType(enuTest2) in a then b should be 6

Thanks
Perry
schneider - 03 Jul 2007 00:07 GMT
Private Sub Test()

      Dim b As Byte = enuTest1.Opt2

  End Sub

>I need to store an enum datatype in a variable and then reference it
>members from the variable. I have tried this but it does not work.
[quoted text clipped - 23 lines]
> Thanks
> Perry
Perecli Manole - 03 Jul 2007 12:43 GMT
What is that? You did not read the comment after the code snipit.
The reason for using variable a is because a will hold an unknown
enumeration which will be suplied by the user (i.e. enuTest1 or enuTest2).
In your example the the enumeration is hard coded.

Perry

>   Private Sub Test()
>
[quoted text clipped - 29 lines]
>> Thanks
>> Perry
schneider - 05 Jul 2007 16:57 GMT
So you are getting the string from the user? You did not state that. Are you
getting a fully qualified enum value?
If you are starting from a string version of the enum value then you should
use Enum.Parse method to get the specified enumeration value.
If the enumeration type could be either #1 or #2 you may need to do further
checks.

> What is that? You did not read the comment after the code snipit.
> The reason for using variable a is because a will hold an unknown
[quoted text clipped - 36 lines]
>>> Thanks
>>> Perry
Mythran - 03 Jul 2007 16:49 GMT
>I need to store an enum datatype in a variable and then reference it
>members from the variable. I have tried this but it does not work.
[quoted text clipped - 23 lines]
> Thanks
> Perry

Try this :)

Imports System.Reflection

Class Class1
   Private Enum enuTest1 As Byte
       Opt1 = 0
       Opt2 = 1
       Opt3 = 2
   End Enum

   Private Enum enuTest2 As Byte
       Opt1 = 5
       Opt2 = 6
       Opt3 = 7
   End Enum

   Public Shared Sub Start()
       Test(GetType(enuTest1), "Opt2")
   End Sub

   Public Shared Sub Test( _
       ByVal EnumType As Type, _
       ByVal MemberName As String _
   )
       If Not EnumType.IsEnum
           Throw New ArgumentException( _
               "Expected an enum type, received " & EnumType.Name & ".", _
               "EnumType" _
           )
       End If

       Dim fieldInfo As FieldInfo = EnumType.GetField(MemberName)
       Dim v As Object = fieldInfo.GetValue(Nothing)
       Console.WriteLine(CInt(v))
   End Sub
End Class

HTH,
Mythran
hav - 04 Jul 2007 06:16 GMT
IEnumerator interface exposed by all collections - returns object you
can GetNext GetEnumerator and reset. dim ALEnum AS IEnumerator
ALEnum = AList.GetEnumerator

dim AItems as IEnu.....tor
aItem = aList.GetEn..r
while aitems.movenext
{ process item aItems.Current }
end while

hth

Henz

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.