473,506 Members | 16,954 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Strongly typed enums

I forgot the syntax for strongly typed enums in .NET. I know C#
supports them. Not sure if strong-typing of enums is CLS compliant but,
can you refresh my memory with the C# syntax and VB syntax, please?

I've been looking in "Professional C#" to no avail yet. I don't
remember where I saw the syntax on an earlier occassion. This is just a
small thing I am trying to memorize.

Nov 20 '05 #1
7 3599
Can't say for C#, but in VB.NET:

Public Enum something As IntegerBasedType
item = number
item= number
item= number
item= number
etc.
End Enum

Note that it is not necessary to manually assign numeric values if you don't
want to. In that case, the values will automatically assigned Integer
values starting from zero.

"Sathyaish" <sa*******@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
I forgot the syntax for strongly typed enums in .NET. I know C#
supports them. Not sure if strong-typing of enums is CLS compliant but,
can you refresh my memory with the C# syntax and VB syntax, please?

I've been looking in "Professional C#" to no avail yet. I don't
remember where I saw the syntax on an earlier occassion. This is just a
small thing I am trying to memorize.

Nov 20 '05 #2
The C# language spec contains information on the enum.
http://msdn.microsoft.com/library/de...pspec_14_3.asp

--
Tim Wilson
..NET Compact Framework MVP

"Sathyaish" <sa*******@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
I forgot the syntax for strongly typed enums in .NET. I know C#
supports them. Not sure if strong-typing of enums is CLS compliant but,
can you refresh my memory with the C# syntax and VB syntax, please?

I've been looking in "Professional C#" to no avail yet. I don't
remember where I saw the syntax on an earlier occassion. This is just a
small thing I am trying to memorize.

Nov 20 '05 #3
"Sathyaish" <sa*******@gmail.com> schrieb:
I forgot the syntax for strongly typed enums in .NET. I know C#
supports them. Not sure if strong-typing of enums is CLS compliant but,
can you refresh my memory with the C# syntax and VB syntax, please?


In addition to the other replies, yes, enumeration types are CLS compilant.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 20 '05 #4
Thanks, everybody.

Just for any one else looking for the same information later, the
syntax, as given by the C# specification is:

enum EnumerationName: DataType
{
elementOne;
elementTwo;
}
e.g.

enum NumeralOrders: uint32
{
tens=10,
hundreds=100,
....
....
millions=1000000,
...
...
thingamajillion=1000000000000
}

Nov 20 '05 #5
Sathyaish wrote:
I forgot the syntax for strongly typed enums in .NET. I know C#
supports them. Not sure if strong-typing of enums is CLS compliant
but, can you refresh my memory with the C# syntax and VB syntax,
please?


Also, I should point out that in C#, all enums are "strongly-type" which
means that a varaible declared as an enum type, can only be that enum type:

enum ABC {A, B, C}
enum XYZ {X, Y, Z}
public static void Main()
{
ABC abc = XYZ.X; // error
ABC def = 2; // error
}

I assume what you are actually after is not a "Strongly-typed enum", but "an
enum with a user-specified underlying type"
--
Truth,
James Curran [erstwhile-MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
Nov 20 '05 #6
"James Curran" <Ja*********@mvps.org> schrieb:
I forgot the syntax for strongly typed enums in .NET. I know C#
supports them. Not sure if strong-typing of enums is CLS compliant
but, can you refresh my memory with the C# syntax and VB syntax,
please?


Also, I should point out that in C#, all enums are "strongly-type" which
means that a varaible declared as an enum type, can only be that enum
type:


This applies to VB.NET with 'Option Strict On' too.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 20 '05 #7
"James Curran" <Ja*********@mvps.org> wrote
Also, I should point out that in C#, all enums are "strongly-type" which
means that a varaible declared as an enum type, can only be that enum
type:

enum ABC {A, B, C}
enum XYZ {X, Y, Z}
public static void Main()
{
ABC abc = XYZ.X; // error
ABC def = 2; // error
}


Yes, but you can do this:

enum ABC {A, B, C}
public static void Main()
{
ABC abc = (ABC)42; // no error
}

From the C# spec (14.5): "The set of values that an enum type can take on is
not limited by its enum members. In particular, any value of the underlying
type of an enum can be cast to the enum type, and is a distinct valid value
of that enum type."
http://msdn.microsoft.com/library/de...pspec_14_5.asp

Regards,
Marcus
Nov 21 '05 #8

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

Similar topics

4
5718
by: Bill Cohagan | last post by:
I'm trying to figure out the "best" way to implement a strongly typed ArrayList. Using inheritance is one approach. It has the advantage that I only have to write overrides for the Add method and...
1
6058
by: Job Lot | last post by:
I am confused how strongly typed dataset is different from un-typed dataset. Is there any good link explaining pros and cons of both? Which one should be used preferably?
2
2361
by: Mark | last post by:
Just wanted to confirm that my understanding of a strongly typed language is correct: 1. .NET is a strongly typed language because each variable / field must be declared a specific type (String,...
1
2163
by: HardBap | last post by:
I've created a strongly typed DataSet (Customers.xsd) using the xsd.exe tool. I want to be able to access fields using ds.Customer.CompanyName. The problem is when I return this DataSet from a...
20
5947
by: Dennis | last post by:
I use the following code for a strongly typed arraylist and it works great. However, I was wondering if this is the proper way to do it. I realize that if I want to implement sorting of the...
7
1076
by: Sathyaish | last post by:
I forgot the syntax for strongly typed enums in .NET. I know C# supports them. Not sure if strong-typing of enums is CLS compliant but, can you refresh my memory with the C# syntax and VB syntax,...
3
1512
by: Polaris431 | last post by:
In code, you can type the following: this.Response.Redirect("~/index.aspx"); Is there anything in ASP.NET that you can use in place of strings when referring to URLs? It would be nice to refer...
1
3538
by: Code Monkey | last post by:
Silly question maybe, but I've been doing the following for far too long now: public static DataTable myDataTable() { string sql = @"SELECT column1, column2, column3, column4 FROM...
4
3091
by: Rachana | last post by:
Hi, I have understood Data Sets but what is meant by typed/untyped/ strongly typed datasets. Can any one explain me or suggest any site/ article, to get these concepts (and their ...
0
7218
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,...
0
7103
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
7370
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...
0
7478
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...
1
5035
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...
0
4701
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...
0
1532
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
409
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...

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.