473,666 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 SomeUInt32Const ants: uint
{
ThisConstant = 0x01121,
ThatConstant= 0x01123,
}

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

public void OperationDepend ingOnParamter(u int uiAconstantValu e)
{

}

I cannot make the following call,
OperationDepend ingOnParamter(S omeUInt32Consta nts.ThisConstan t). 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
SomeUInt32Const ants.ThisConsta nt 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 1306
"SpotNet" <Sp*****@msnews .grpwrote in
news:eI******** ******@TK2MSFTN GP04.phx.gbl:
I cannot make the following call,
OperationDepend ingOnParamter(S omeUInt32Consta nts.ThisConstan t). 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
SomeUInt32Const ants.ThisConsta nt 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@makeDInt oDot_ctiusaDcom wrote in message
news:Xn******** *************** *****@207.46.24 8.16...
: "SpotNet" <Sp*****@msnews .grpwrote in
: news:eI******** ******@TK2MSFTN GP04.phx.gbl:
:
: I cannot make the following call,
: OperationDepend ingOnParamter(S omeUInt32Consta nts.ThisConstan t). 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
: SomeUInt32Const ants.ThisConsta nt 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
9544
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 change and lead to memory corruption. I didn't see how this was possible. He claims that if a dll built for a program is built with different compiler settings than the launcher program, the enum size could change. The only way I could figure...
27
2725
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 <typename Enum> class smart_enum { public: typedef Enum enum_type;
0
1322
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 needed/created enum a property spec will be defined - which will feed a PropertyBag. Problem: My Problem is that the PropertyGrid wont display a DropDown Menu for a new created Instance but give me an exception.
4
5588
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 when looking at the db we can't tell what the value in a field represents. The other problem is that our enums are currently all stored in a single class, which means that because of no visibility constraints the enums are often used out of context...
1
2100
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
1408
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 where it should reside. I'll use Enum for this example. Currently in a Public Module: ---------------------------- Public Enum MyUserDefinedType
37
2205
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. Thanks in advance
7
5256
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 users of the lib dont require all the development tools we used to develop the lib. I have noticed something strange, and was wondering if its a C++ feature, or a implementation specific bug. I have a function like this:
9
2721
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 execution speed." feels like typical ultra high level scripting language design. The compiler could not optimize an enum if it wanted to, because you cannot even specify whether memory requirements or speed are your primary concern. Enums were meant to...
0
8448
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
8356
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
8871
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...
1
8552
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,...
1
6198
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
4198
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
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1776
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.