473,385 Members | 1,780 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.

Enums and types, does the enum really take the type?


Hello NewsGroup,

All the best for the New Year to you all. Have a question regarding enums
and the integer types that can be scoped as. Briefly, if I scope an enum as
uint, I cannot use the enum as a uint may be used generally, but must use it
as the enum.

For example:

public enum SomeUInt32Constants: uint
{
ThisConstant = 0x01121,
ThatConstant= 0x01123,
}

A function that has a uint parameter, or a value from SomeUInt32Constants
enum.

public void OperationDependingOnParamter(uint uiAconstantValue)
{

}

I cannot make the following call,
OperationDependingOnParamter(SomeUInt32Constants.T hisConstant). I can, but I
receive the appropriate casting error. I know this question is academic in
nature, though I ask on a pratical reason, is
SomeUInt32Constants.ThisConstant a uint (UInt32)? Please I'm aware of the
various approaches in passing a predefined constant (as well as any other
value not defined), my question is purely on the basis asked.

Thank you all very much NewsGroup, your responses are very much appreciated.

Regards and Happy New Year to you all,
SpotNet.
Jan 1 '07 #1
2 1295
"SpotNet" <Sp*****@msnews.grpwrote in
news:eI**************@TK2MSFTNGP04.phx.gbl:
I cannot make the following call,
OperationDependingOnParamter(SomeUInt32Constants.T hisConstant). I can,
but I receive the appropriate casting error. I know this question is
academic in nature, though I ask on a pratical reason, is
SomeUInt32Constants.ThisConstant a uint (UInt32)? Please I'm aware of
the various approaches in passing a predefined constant (as well as
any other value not defined), my question is purely on the basis
asked.
This is because .NET considers enum's to be "a different type" from the
underlying int/uint/long/etc value... its primarily for type safety, to
prevent you from doing something you didn't mean to do. Because a uint can
be anything, but the enum can only be certain values, so from a number-set
standpoint, they are different.

If you want to pass in a uint to the function, you can cast it as a uint
and it will work fine. As you say, a possibly better approach is to define
these values a const uints in a class.

-mdb
Jan 1 '07 #2

Thank you very much Michael for the explaination.

Regards,
SpotNet.

"Michael Bray" <mbray@makeDIntoDot_ctiusaDcomwrote in message
news:Xn****************************@207.46.248.16. ..
: "SpotNet" <Sp*****@msnews.grpwrote in
: news:eI**************@TK2MSFTNGP04.phx.gbl:
:
: I cannot make the following call,
: OperationDependingOnParamter(SomeUInt32Constants.T hisConstant). I can,
: but I receive the appropriate casting error. I know this question is
: academic in nature, though I ask on a pratical reason, is
: SomeUInt32Constants.ThisConstant a uint (UInt32)? Please I'm aware of
: the various approaches in passing a predefined constant (as well as
: any other value not defined), my question is purely on the basis
: asked.
: >
:
: This is because .NET considers enum's to be "a different type" from the
: underlying int/uint/long/etc value... its primarily for type safety, to
: prevent you from doing something you didn't mean to do. Because a uint
can
: be anything, but the enum can only be certain values, so from a number-set
: standpoint, they are different.
:
: If you want to pass in a uint to the function, you can cast it as a uint
: and it will work fine. As you say, a possibly better approach is to
define
: these values a const uints in a class.
:
: -mdb
Jan 1 '07 #3

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

Similar topics

13
by: SpaceCowboy | last post by:
I recently got into a discussion with a co-worker about using enums across a dll interface. He wanted to use chars instead, argueing that depending on compiler settings the size of an enum could...
27
by: Mark A. Gibbs | last post by:
i have been toying with the idea of making my enums smarter - ie, more in line with the rest of the language. i haven't tested it yet, but what i came up with is a template like this: template...
0
by: TRUMPh | last post by:
Context: I have a list with indeterminately entrys @design time. The user should choose one of the item via property grid. I create a classes and a instances of enums @RunTime. For each...
4
by: Martin Pritchard | last post by:
Hi, I'm working on a project that historically contains around 40 enums. In the database various fields refer to the int values of these enums, but of course ref integrity is not enofrced and...
1
by: Harold Hsu | last post by:
Hi, Originally, I have defined the following Enum within a namespace (outside a class): Public Enum UserRole Admin Manager User End Enum
2
by: Paul | last post by:
I have a situation that I want to work but don't know the proper way to handle it. I want to create a User Defined "Type" (structure, class or enumeration). I'm not sure what it should be or...
37
by: mdh | last post by:
In one of the answers to a K&R exercise, the first couple of lines are: enum loop { NO, YES}; enum loop okloop=YES; I get the first line, but not the second. Sorry about the LOL question. ...
7
by: Kurt | last post by:
Hi. I have a c++ project that is basically a set of implementation hiding classes, so that we can convert a static lib with a lot of dependancies into a dynamic lib with less dependancies, so that...
9
by: copx | last post by:
C's enum type disappoints me a lot. You cannot define which type of integer variable is used. This contradicts C's low level spirit. "I want a number variable. I do not care about size or...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
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
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.