473,500 Members | 1,686 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how can I set an enum to be null?

I have an enumerated type

public enum Measured

{

N,
}

when I create a variable of that type

private Measured measuredVar;

I can easily set it to be N with measuredVar = Measured.N;

However sometimes I want measured to be null. Is this possible?

jamie


May 5 '06 #1
4 28104
In .NET 1.1, no... you had to create a value in your enum series for
"not specified."

In .NET 2.0, you should use nullable types. Declare your variable like
this:

private Measured? measuredVar;

The question mark indicates that the value is nullable.

May 5 '06 #2
No. An Enum is a value type, not a reference type, so there is no
reference to set to null.

In .NET 2.0 you can use a nullable variable:

private Measured? measuredVar;

Now you can set measuredVar to Measured.N value or to null.
jamie wrote:
I have an enumerated type

public enum Measured

{

N,
}

when I create a variable of that type

private Measured measuredVar;

I can easily set it to be N with measuredVar = Measured.N;

However sometimes I want measured to be null. Is this possible?

jamie

May 5 '06 #3
Hello Jamie,

enum is a value type. You cannot set it to null.
--
Alexander Todorovic
"jamie" wrote:
I have an enumerated type

public enum Measured

{

N,
}

when I create a variable of that type

private Measured measuredVar;

I can easily set it to be N with measuredVar = Measured.N;

However sometimes I want measured to be null. Is this possible?

jamie


May 5 '06 #4
"jamie" <st*********@nospam.nospam> wrote:
I have an enumerated type
public enum Measured { N, ..}
when I create a variable of that type
private Measured measuredVar;
I can easily set it to be N with measuredVar = Measured.N;
However sometimes I want measured to be null. Is this possible?


I think that best practise in this case is

public enum Measured {None=0, N, M, ...};

This way,
Measured x = default(Measured);
will product x == Measured.None.

I think that using nullable types for this would not be worth it.

--
Lucian
May 5 '06 #5

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

Similar topics

18
2342
by: Nebula | last post by:
Consider enum Side {Back,Front,Top,Bottom}; enum Side a; Now, why is a = 124; legal (well it really is an integer, but still, checking could be performed..) ? Wouldn't enums be more useful if...
3
3501
by: Zoury | last post by:
Hi There! :O) I've been trying this sample over here : http://groups.google.com/groups?threadm=urQH9LbPCHA.2420%40tkmsftngp11 Unfortunately the GetNestedType() function returns null no matter...
31
3561
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();...
18
11295
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
6
13623
by: Vern | last post by:
I wanted to create an enum with the following: SSN, EIN, N/A But it doesn't like the /. How can I put / or ? characters into the list. Also is it possible to have blanks as a valid enum? or...
2
6784
by: Robert W. | last post by:
In a posting earlier this year I found a simple approach to convert a string to a particular Enum value. The one line solution looked like this: MyEnum ConvertedString = (MyEnum)...
7
9801
by: Harris | last post by:
Dear all, I have the following codes: ====== public enum Enum_Value { Value0 = 0, Value1 = 10,
34
11130
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...
35
15140
by: dtschoepe | last post by:
Greetings. I am working on an assignment and can't seem to get the right concept for something I'm attempting to do with enum data types. I have defined the following in my code: enum color...
3
3026
by: hufaunder | last post by:
Imagine you have a charting library that can draw lines, bars, floating bars, bands, etc. Lines and bars need only one input. Floating bars and bands need two inputs. There are two approaches: ...
0
7134
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
7014
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
7229
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...
1
6905
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...
0
7395
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...
0
5485
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,...
0
3108
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...
0
3103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.