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 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.
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
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
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 ?
|
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!
|
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;
|
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
|
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?
| |
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...
|
by: Harris |
last post by:
Dear all,
I have the following codes:
======
public enum Enum_Value
{
Value0 = 0,
Value1 = 10,
|
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?
|
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
|
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...
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
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();...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |