By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,578 Members | 1,626 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,578 IT Pros & Developers. It's quick & easy.

VB vs C#: int values automatically converted to enum types

P: n/a
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

Nov 21 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
<ar******@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/>

Nov 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.