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

A

arzewski

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
 
H

Herfried K. Wagner [MVP]

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#.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top