473,385 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

Nov 21 '05 #1
1 1626
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: David Sworder | last post by:
With a standard enum, values are automatically assigned. public enum MyEnum{Happy,Sad,Angry,Grandpa}; ....but what about a bitflag enum? public enum MyEnum:int{ FalseTeeth=0x00000001...
18
by: Nebula | last post by:
Consider enum Side {Back,Front,Top,Bottom}; enum Side a; Now, why is a = 124; legal (well it really is an integer, but still, checking could be performed..) ? Wouldn't enums be more useful if...
31
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1();...
1
by: arzewski | last post by:
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...
1
by: Maziar Aflatoun | last post by:
Hi, I have the lKey object that can be used to enable different operation systems such Windows 2000 Linux FreeBSD etc. Now I want to read a database table that contains key/values and if...
22
by: Ben Finney | last post by:
Howdy all, I've recently packaged 'enum' in PyPI. In its description, I make the claim that it creates "immutable" enumeration objects, and that the enumeration values are "constant" values. ...
6
by: cipher | last post by:
I have some constant values in my web service that my client application will require. Having to keep server side and client side definitions insync is tedious. I am trying to do something like...
27
by: Ben Finney | last post by:
Antoon Pardon wrote: > I just downloaded your enum module for python > and played a bit with it. IMO some of the behaviour makes it less > usefull. Feedback is appreciated. I'm hoping to...
9
by: Fred Zwarts | last post by:
What is the recommended way to loop over all enum values of a certain enum type? Consider the following definition: typdef enum {A=2, B, C=5, D} E; then for (E x = A; x <= D; ++x) { ... }
11
by: Jon Slaughter | last post by:
This is more of a C++ question but I guess it applies equally well to C#. Is there a way to declare an enum where the values assigned to the fields is incremented by something rather than 1. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.