gi-m

If I declare, for example, a Nullable Boolean type and assign it to a value, let's say true, how do I set it back to null if I want to . I tried NULL, nullptr, and I searched around the documentation a little bit and didn't find the answer. The best I could come up with that works is the following:

Code Snippet

Nullable<Boolean> nullbool;

Nullable<Boolean> nb = true;

nb = nullbool;

is there an equivalent keyword or constant in C++ to C# null or VB Nothing


Re: Visual C++ Language Setting a Nullable type to null in C++

crescens2k

It should be nullptr, according to the text for the nullptr keyword.

nullptr is equivalent to Nothing in Visual Basic and null in C#. Function calls among languages that use these null mechanisms for error checking should be interpreted correctly.

After looking around a little, the way to set a nullable type to null in C++ is actually rather annoying.

You have to use the following.

Nullable<Boolean> nb;

nb = Nullable<Boolean>();






Re: Visual C++ Language Setting a Nullable type to null in C++

gi-m

Annoying, but it works. Thank you very much. I also saw the nullptr on-line documentation that says what you quoted. A bit misleading.

Where did you find this way of doing it

After looking around a bit more myself I found the following even more annoying way of assigning null to a Nullable in C++, on the Generics:FAQ (http://msdn2.microsoft.com/en-us/library/Aa479866.aspx#netframework_topic15page) I found the following code:

Code Snippet

Nullable<int> number = 123;
Debug::Assert(number.HasValue);
number = Nullable<int>::FromObject((Object ^)nullptr);
Debug::Assert(number.HasValue == false);
Debug::Assert(number.Equals(null));

and after looking around a little more it seems that this code is old and does not compile anymore ! It seems (form the little bit I was able to surmise from searching around) that FromObject used to be a method in Nullable and was changed/removed at some point (search for FromOject in the forums and see some users that got caught in the fold of this change). So now the compiler says that FromObject is not a member of Nullable<T> anymore. So we are back to your technique which works and is less annoying. Thanks again . (and again where did you find it )

PS: If you don't mind I think that all this deserves a Connect submission so I am going going to submit the following issues through Connect(unless you want to) (a) to clarify the use of nullptr (b) to add documentation for your method of setting Nullables to null in C++ (c) to correct the code snippet in the Generic FAQ.





Re: Visual C++ Language Setting a Nullable type to null in C++

crescens2k

Well, I originally got this idea from an article by Nishant Sivakumar and looking in the MSDN for information on the Nullable structure confirmed this. The Nullable struct has an op_Implicit operator, which is an implicit conversion operator. So if you use this and assign an empty Nullable struct, created with the Nullable<T>(), then it will put the null value in.

But you can go ahead and suggest this at connect if you wish.