473,624 Members | 2,439 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 28108
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*********@no spam.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(Measure d);
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
2366
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 there was a bit more typechecking ?
3
3523
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 what enum member i'm passing to it. Does any one has an idea why? We kind of need to build an enum one the fly and this sample seemed to be the way to go. TIA!
31
3586
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
11339
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
13660
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 Null?
2
6799
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) Enum.Parse(typeof(MyEnum), MyString, true); This is fine if one wants to hardcode each and every Enum in an If-ElseIf or Select-Case construct but I'm wondering if there's a way to do it generically? I have a situation where I'm using reflection to set a...
7
9823
by: Harris | last post by:
Dear all, I have the following codes: ====== public enum Enum_Value { Value0 = 0, Value1 = 10,
34
11173
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?
35
15235
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 {red, blue, green, yellow, black, purple, pink}; What I am doing now is reading from a text file a string (char *). I
3
3035
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: 1) One enum with all 4 types (bars, band, etc). One chart class that accepts up to 2 arrays of values. If the user choses a band but there is only one input array throw an exception. If the user passes two input arrays with different lengths throw...
0
8175
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
8680
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...
0
8625
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
8336
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
8482
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
7168
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...
0
5565
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();...
0
4177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1791
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.