Hi all,
I am a VB6 programmer and learning C#. I am currently reading a chapter
on types. I have question regarding enums. Why do we need to convert enum
members to the value that they represent?
Thanks in advance...
-Nikhil 4 6459
Thanks for the reply.
I think I didnt give more details on my question. Let me give an
example:
public enum Numbers
{
zero=0,
one=1,
two=2
}
now if I want to use Numbers.two in mathematical calculations, I need to
explicitly convert it to int as follows,
MessageBox.Show(((int)Numbers.two * 2).ToString()); //Displays 4
But if we use the Numbers enum in switch statement, we dont have to convert
it.
So my question is why do we have to convert the members in some cases.
VB.Net does not require the implicit conversion of the enum numbers even if
you use them in mathematical calculations.
Thanks...
-Nikhil
"Dmitri Shvetsov" <ds*******@cox.net> wrote in message
news:F%ESa.7423$Ne.7231@fed1read03... It can be very helpful when you send these enums through interfaces for example and these enum types are unknown in another objects. Then if you use integer values for example in pairs to these enums you can convert these values to integers and back, use in switch statements, somewhere else. In 2 words - it's more flexible.
My example. I use these enums to define some errors in method parameters
and can send these eror codes through the COM+ interface if I need. Then I can use a switch based method to get an error string for any defined enum values.
public enum Parameter { E_Invalid = 1000, E_IsNull = 1001, E_IsEmpty = 1002, E_NothingToDo = 1003 };
It depends what really do you need. Sometimes you can use an array of
enums without integer analogs and this will also work.
Regards, Dmitri.
"Nikhil Patel" <ni********@aol.com> wrote in message news:%2****************@tk2msftngp13.phx.gbl... Hi all, I am a VB6 programmer and learning C#. I am currently reading a chapter on types. I have question regarding enums. Why do we need to convert
enum members to the value that they represent?
Thanks in advance...
-Nikhil
why? why?
SECURITY man. More RULES. RED TAPE. Or incapacitation if you look closer.
Don't enter the MATRIX! Don't lose your freedom! Stay with VB or better jump
to C++ if you want to know what FREESTYLE is.
open your eyes!
"Nikhil Patel" <ni********@aol.com> wrote in message
news:#u**************@tk2msftngp13.phx.gbl... Hi all, I am a VB6 programmer and learning C#. I am currently reading a
chapter on types. I have question regarding enums. Why do we need to convert enum members to the value that they represent?
Thanks in advance...
-Nikhil
[If this post comes through twice, but with a different last sentence,
take this one as the more correct one :) ]
Nikhil Patel <ni********@aol.com> wrote: I am a VB6 programmer and learning C#. I am currently reading a chapter on types. I have question regarding enums. Why do we need to convert enum members to the value that they represent?
Type safety, basically. You have to prove to the compiler that you
really know that the variable in question is an enum and not an int,
but that in this case you want to treat it as an int, or vice versa.
Sometimes it gets in the way, other times it's useful.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nikhil,
Learn to use Option Strict On in VB.NET! :-)
With option strict on VB.NET does require the explicit conversion of enums,
to perform math. The only 'math' operator defined for Enum types in VB.NET
is the Or operator to combine Enums that have the Flags attribute applied
for example. Of course you can compare two Enums and assign an Enum also.
You should normally use Option Strict On at the top of each of your VB.NET
files, to ensure compile time errors over run time errors. As Jon Skeet
stated, type safety!
Hope this helps
Jay
"Nikhil Patel" <ni********@aol.com> wrote in message
news:OU**************@TK2MSFTNGP10.phx.gbl... error in last post.
Please read this line... I said VB.Net does not require the implicit conversion of the enum numbers even if you use them in mathematical calculations. Please read it as, VB.Net does not require the explicit conversion of the enum numbers
even if you use them in mathematical calculations.
Sorry about that.
"Nikhil Patel" <ni********@aol.com> wrote in message news:u9**************@TK2MSFTNGP10.phx.gbl... Thanks for the reply. I think I didnt give more details on my question. Let me give an example:
public enum Numbers { zero=0, one=1, two=2 }
now if I want to use Numbers.two in mathematical calculations, I need to explicitly convert it to int as follows,
MessageBox.Show(((int)Numbers.two * 2).ToString()); //Displays 4
But if we use the Numbers enum in switch statement, we dont have to convert it.
So my question is why do we have to convert the members in some cases. VB.Net does not require the implicit conversion of the enum numbers even if you use them in mathematical calculations.
Thanks...
-Nikhil
"Dmitri Shvetsov" <ds*******@cox.net> wrote in message news:F%ESa.7423$Ne.7231@fed1read03... It can be very helpful when you send these enums through interfaces
for example and these enum types are unknown in another objects. Then if you use integer values for example in pairs to these enums you can convert these values to integers and back, use in switch statements, somewhere else. In 2 words - it's more flexible.
My example. I use these enums to define some errors in method
parameters and can send these eror codes through the COM+ interface if I need. Then I can use a switch based method to get an error string for any defined enum values.
public enum Parameter { E_Invalid = 1000, E_IsNull = 1001, E_IsEmpty = 1002, E_NothingToDo = 1003 };
It depends what really do you need. Sometimes you can use an array of
enums without integer analogs and this will also work.
Regards, Dmitri.
"Nikhil Patel" <ni********@aol.com> wrote in message news:%2****************@tk2msftngp13.phx.gbl... > Hi all, > I am a VB6 programmer and learning C#. I am currently reading a chapter > on types. I have question regarding enums. Why do we need to convert enum > members to the value that they represent? > > Thanks in advance... > > -Nikhil > >
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: AngleWyrm |
last post by:
"The C++ Programming Language" by Bjarne Stroustrup, copyright 1997 by AT&T,
section 4.8 (pp 77):
"A value of integral type may be explicitly converted to an enumeration
type. The result of such a...
|
by: Michel |
last post by:
Hi there,
I have an enum field that I serialize:
public ParameterDirection Direction = ParameterDirection.Out;
The enum looks like this:
public enum ParameterDirection { In, Out, Both };
|
by: Andreas Huber |
last post by:
Hi there
Spending half an hour searching through the archive I haven't found a
rationale for the following behavior.
using System;
// note the missing Flags attribute
enum Color
{
|
by: Hans De Schrijver |
last post by:
Is it possible to extend the members of an Enum defined in an abstract class
in a derived class?
For instance:
abstract class class1
{
protected enum MyEnum {A, B, C}
}
public class class2...
|
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();...
|
by: Visual Systems AB \(Martin Arvidsson\) |
last post by:
Hi!
I have created an enum list like this:
enum myEnum : int
{
This = 2,
That,
NewVal = 10,
LastItm
|
by: Don |
last post by:
How do I get an Enum's type using only the Enum name?
e.g.
Dim enumType as System.Type
Dim enumName as String = "MyEnum"
enumType = ???(enumName)
|
by: Steven Nagy |
last post by:
So I was needing some extra power from my enums and implemented the
typesafe enum pattern.
And it got me to thinking... why should I EVER use standard enums?
There's now a nice little code...
|
by: jonpb |
last post by:
The documentation for the "is" keyword says:
An is expression evaluates to true if the provided expression is
non-null, and the provided object can be cast to the provided type
without causing...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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...
|
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,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |