Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old June 4th, 2003, 03:33 PM
Registered User
 
Join Date: Jun 2003
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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,
  #2 (permalink)  
Old June 4th, 2003, 06:10 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Wien, Wien, Austria.
Posts: 158
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to NotNowJohn
Default

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
  #3 (permalink)  
Old June 5th, 2003, 05:20 AM
Authorized User
 
Join Date: Jun 2003
Location: , , .
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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,


Similar Threads
Thread Thread Starter Forum Replies Last Post
enum 2 names OneQuestion General .NET 4 January 2nd, 2008 02:50 PM
Enum gets initalized to first value mhshpk ASP.NET 2.0 Professional 2 September 4th, 2007 10:16 AM
enum toString() overloading kramis8 C# 0 November 21st, 2004 07:55 PM
Enum in VB saeta119 BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 2 June 7th, 2003 04:10 PM





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