Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

VB vs C#: int values automatically converted to enum types
I don't know what was the reason from a language design point-of-view
to allow this, but VB.NET compiler does not flag you (when C# compiler
does) when passing an integer value as a parameter to a function that,
from its signature, it is expecting an enumerated type.

In the code below, invoking SetGender(2) will set a female.  What if
later on the order of the enum types declared are changed such that
invoking the same code would then set a male instead ?

--- code snippets ---

Public Enum Gender
Undefined
Male
Female
End Enum

Private Sub GenderButtonClicked(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnGender.Click

Dim strValue As String = txtGender.Text.ToUpper()

'Me.SetGender(strValue)

Select Case strValue
Case "MALE", "M", "XY"
Me.SetGender(Gender.Male)
Case "FEMALE", "F", "XX"
Me.SetGender(Gender.Female)
Case Else
'If (FormEnum.IsStringNumeric(strValue)) Then
If (FormEnum.IsNumeric(strValue)) Then
Dim intValue As Integer = Int32.Parse(strValue)
Me.SetGender(intValue)
Else
Me.SetGender(Gender.Undefined)
End If
End Select

End Sub

Private Sub SetGender(ByVal param As Gender)

txtMessage.Clear()

Select Case param
Case Gender.Male
rbnMale.Checked = True
Case Gender.Female
rbnFemale.Checked = True
Case Gender.Undefined
txtMessage.Text = "value not a gender"
Case Else
txtMessage.Text = "int value not in the enum set"
End Select
End Sub

'lets suppose that this function that is expecting a string does
not exist
'interestingly, if this function existed, then invoking the
SetGender function and passing
'an int would have the compiler generate an error because it
wouldn't know how to
'choose between the one with Gender signature or the one with
String signature
'But with only one function existing (the one with Gender
signature), the compiler
'doesn't complain and just converts the int to the associated int
value in the enum
'Private Sub SetGender(ByVal param As String)

'    Select Case param.ToUpper()
'        Case "MALE", "M", "XY"
'            Me.SetGender(Gender.Male)
'        Case "FEMALE", "F", "XX"
'            Me.SetGender(Gender.Female)
'        Case Else
'            If (FormEnum.IsStringNumeric(param)) Then
'                Dim intValue As Integer = Int32.Parse(param)
'                Me.SetGender(intValue)
'            Else
'                Me.SetGender(Gender.Undefined)
'            End If


'    End Select

'End Sub


Report this thread to moderator Post Follow-up to this message
Old Post
arzewski@hotmail.com
05-06-05 08:58 PM


Re: VB vs C#: int values automatically converted to enum types
<arzewski@hotmail.com> schrieb:
>I don't know what was the reason from a language design point-of-view
> to allow this, but VB.NET compiler does not flag you (when C# compiler
> does) when passing an integer value as a parameter to a function that,
> from its signature, it is expecting an enumerated type.
>
> In the code below, invoking SetGender(2) will set a female.  What if
> later on the order of the enum types declared are changed such that
> invoking the same code would then set a male instead ?
>
> --- code snippets ---
>
> Public Enum Gender
>    Undefined
>    Male
>    Female
> End Enum

Either set 'Option Strict On' in the project properties or add the line on
top of the source file.  This will enable strict semantics which make VB.NET
behave similar to C#.

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


Report this thread to moderator Post Follow-up to this message
Old Post
Herfried K. Wagner [MVP]
05-06-05 08:58 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

C# archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 09:55 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.