473,770 Members | 2,171 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 GenderButtonCli cked(ByVal sender As System.Object,
ByVal e As System.EventArg s) Handles btnGender.Click

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

'Me.SetGender(s trValue)

Select Case strValue
Case "MALE", "M", "XY"
Me.SetGender(Ge nder.Male)
Case "FEMALE", "F", "XX"
Me.SetGender(Ge nder.Female)
Case Else
'If (FormEnum.IsStr ingNumeric(strV alue)) Then
If (FormEnum.IsNum eric(strValue)) Then
Dim intValue As Integer = Int32.Parse(str Value)
Me.SetGender(in tValue)
Else
Me.SetGender(Ge nder.Undefined)
End If
End Select

End Sub

Private Sub SetGender(ByVal param As Gender)

txtMessage.Clea r()

Select Case param
Case Gender.Male
rbnMale.Checked = True
Case Gender.Female
rbnFemale.Check ed = True
Case Gender.Undefine d
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(Ge nder.Male)
' Case "FEMALE", "F", "XX"
' Me.SetGender(Ge nder.Female)
' Case Else
' If (FormEnum.IsStr ingNumeric(para m)) Then
' Dim intValue As Integer = Int32.Parse(par am)
' Me.SetGender(in tValue)
' Else
' Me.SetGender(Ge nder.Undefined)
' End If
' End Select

'End Sub

Nov 17 '05 #1
1 1942
<ar******@hotma il.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 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1514
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 BrainCancer=0x00000002
18
2382
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 there was a bit more typechecking ?
31
3616
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(); Control c = f; An enum value inherits from int but it doesn't get implicitly converted: HorizontalAlignment h = HorizontalAlignment.Center;
1
1133
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 the key
1
1642
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 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...
22
2139
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. This raises questions. Is there any difference between a Python immutable value, and a constant? I suppose "constant" also implies that the *name* binds
27
2677
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 provide a "one obvious way" to do enumerations in Python. > >>> from enum import Enum > >>> day = Enum('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun')
9
53803
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
1730
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. What I would like to do is create an enum where the varaibles increment by some formula rather than just 1. I doubt this is possible but it would be nice Essentially what I'm having to do is
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10260
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10102
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6712
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.