3.3 API Changes and more - Nullable is not null?
All VistaDBType objects are really nullable types. Why? Because they come from your database. If your database is set to allow nulls, any of them could be set to a null. This is not the same as the dot net runtimes definition of a nullable type though. In dot net you can set an object to be nullable meaning it has no value, but it does not make sense to do that same for a column in a database. Either it is a column or it is not. But for the value of a column, it does make sense.
GetValueOrDefault() – new API
For a dot net variable that is nullable you get a new method and property to help you manipulate the object. The HasValue property is a Boolean that tells if the object is assigned to null. And you get a method GetValueOrDefault(). The overloads vary depending upon if you have a reference type or a value type. For all of the VistaDBTypes we have implemented several overloads. The empty method will simply return a new system type.
So if you have a VistaDBBoolean and it is internally set to null, calling GetValueOrDefault() simply returns a new bool(). This is probably the least useful because you are not really controlling the return value (you get a system default).
GetValueOrDefault( SystemType ) is the next overload. You may pass in an initialized system type for the object and it will be returned to you if the object is null. GetValueOrDefault( false ) will return false if the object is null.
GetValueOrDefault( VistaDBType ) is the final overload. In this case you may have an existing VistaDBType you want to use as the default. GetValueOrDefault( myBoolVar ) would return the internal value of myBoolVar (as a Boolean, no casting required).
Speaking of casting…
And while I am on the subject of casting let me just say that we have not given up on the casting of types. I have experimented with writing explicit and implicit operator = overloads, using generics, and implementing nullable interfaces on the internal Value object. None of them are better than the current scenario (in my opinion). So while we don’t have a solution yet, I did want to take a minute and explain a better way to cast that many people don’t know about. Not all types of casting are the same.
- Somevar = VarFromDatabase as VistaDBBoolean;
- Somevar = (VistaDBBoolean) VarFromDatabase;
What is the difference in these two? The assignment AS a type will always succeed without a runtime exception. But if the cast is invalid you will get a null in the variable instead of what you were expecting. The specified cast can blow up on you at runtime if your object cannot be cast into that type.
Fundamentals of nullable types in Dot Net 2 and higher
Finally I would like to take a minute to discuss nullable types in Dot Net 2 and higher. The key thing to remember about nullable types in the runtime is that only value types support the Nullable interface because reference types have always been able to be assigned null.
- String Somevar = null; // Always been supported
- Int Somevar = null; // Error – value types cannot be assigned to null
This is a nullable type in C#.
- Int? Somevar = null;
How do you determine if it is null? Variables that implement the Nullable interface will have a property HasValue which returns a bool value telling you if the object has been assigned a value or is null. This is not the same thing as assigning a legitimate value, just that some value has been assigned that is not null. You could still generate runtime exceptions for assigning bad values.
Another syntax you might find useful
There is another new conditional syntax that many people don’t know about for conditionally assigning an object.
Int? x = null;
Int y = 30;
Int z = x ?? y;
In this case z gets assigned 30. The ?? operator returns the left hand operand if it is not null, or the right operand. It is a more compact conditional operator. This is how you used to write the same thing.
Int z = (x != null) ? x : y;
The ?? operator was built specifically for working with nullable types. It makes assigning a variable, or a default very easy.
[UPDATE Jan 2008] Here is a great article on ?? Operator chaining.
Similar Posts
- SQL Server 2008 (Katmai) Information
- The GC does not solve all memory leaks
- devLINK 2007 – post show analysis
