p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   VB.NET 2002/2003 Basics (http://p2p.wrox.com/forumdisplay.php?f=76)
-   -   Using Enum Type (http://p2p.wrox.com/showthread.php?t=138)

Mike2000 June 4th, 2003 03:33 PM

Using Enum Type
 
I have Enum Type defined as followes:

Public Enum GenderType
    Male = 0
    Female = 1
End Enum

1)I am using following code to get Gender as input from user. If user entered 0 or 1 then program accepts but if user entered Male or Female it gives problem ? how the following code could be altered to work the way it accepts Male or Female as input.

        Dim PromptForGender as GenderType
        Console.Write("Please enter your gender: ")
        PromptForGender = Console.ReadLine

2)The other problem is when I tried to print from the object this gender type like contacts.GenderType it print 0 or 1 as it stored there. How I can show Male or Female instead of 0 or 1.

Help would be appreciated.

Thanks,

NotNowJohn June 4th, 2003 06:10 PM

You can use only ToString() method to display Enum value instaed of integer. Some example:

Public Enum GenderType
      Male = 0
      Female = 1
End Enum

Dim gt As GenderType
gt=1
Console.WriteLine(gt.ToString())

On console will be displayed Female

HTH.

Reg,
NotNowJohn

...but the Soon is eclipsed by the Moon

Phillip June 5th, 2003 05:20 AM

I think you can use:

Public Enum GenderType as string
    Male = "male"
    Female = "Female"
End Enum

Quote:

quote:Originally posted by Mike2000
 I have Enum Type defined as followes:

Public Enum GenderType
    Male = 0
    Female = 1
End Enum

1)I am using following code to get Gender as input from user. If user entered 0 or 1 then program accepts but if user entered Male or Female it gives problem ? how the following code could be altered to work the way it accepts Male or Female as input.

        Dim PromptForGender as GenderType
        Console.Write("Please enter your gender: ")
        PromptForGender = Console.ReadLine

2)The other problem is when I tried to print from the object this gender type like contacts.GenderType it print 0 or 1 as it stored there. How I can show Male or Female instead of 0 or 1.

Help would be appreciated.

Thanks,


All times are GMT -4. The time now is 12:15 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.