473,695 Members | 3,116 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why enum members need conversion

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
Nov 15 '05 #1
4 6483
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@fed1re ad03...
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******** ********@tk2msf tngp13.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


Nov 15 '05 #2
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******** ******@tk2msftn gp13.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


Nov 15 '05 #3
[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.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #4
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******** ******@TK2MSFTN GP10.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******** ******@TK2MSFTN GP10.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@fed1re ad03...
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******** ********@tk2msf tngp13.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
>
>



Nov 15 '05 #5

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

Similar topics

9
3130
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 conversion is undefined unless the value is within the range of the enumeration. For example: enum flag { x=1, y=2, z=4, e=8 }; // range 0:15 flag f1 = 5; // type error: 5 is not of type flag flag f2 = flag(5); // ok: flag(5) is of type flag and...
2
2846
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 };
21
4592
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 {
3
10300
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 : class1
31
3599
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;
18
11351
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
13
12381
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)
34
11186
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 snippet that I wrote today that gives me an instant implementation of the pattern. I could easily just always use such an implementation instead of a standard enum, so I wanted to know what you experts all thought. Is there a case for standard enums?
4
2695
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 an exception to be thrown. So, given: enum TestEnum { t1, t2 } TestEnum te = TestEnum.t1;
0
8640
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9001
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...
1
8860
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8832
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7672
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...
1
6498
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5841
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();...
2
2281
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1984
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.