Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to implement a Nullable Type like NullableOfInteger in VB6

Tags:

nullable

vb6

Just wondering if anyone knows of a way to implement a Typed Nullable Type like NullableOfInteger in VB6? (I'm trying to avoid using variants)

You can easily create a custom class NullableOfInteger and use its uninitialized state to indicate a Null state but this comes with the obvious disadvantages.

Beyond that I can't really think of any other ways? My gut tells me there would be no good way.

like image 524
Maxim Gershkovich Avatar asked May 20 '11 02:05

Maxim Gershkovich


People also ask

How do you make a type Nullable?

You can declare nullable types using Nullable<t> where T is a type. Nullable<int> i = null; A nullable type can represent the correct range of values for its underlying value type, plus an additional null value. For example, Nullable<int> can be assigned any value from -2147483648 to 2147483647, or a null value.

Does HasValue check for null?

HasValue: This property returns a bool value based on that if the Nullable variable has some value or not. If the variable has some value, then it will return true; otherwise, it will return false if it doesn't have value or it's null.

Can integer be null VB?

Consider an Integer in VB.NET. It cannot be null—it can only have a numeric value.

How do you access the underlying value of a nullable type?

You cannot directly access the value of the Nullable type. You have to use GetValueOrDefault() method to get an original assigned value if it is not null. You will get the default value if it is null. The default value for null will be zero.


1 Answers

VB6 doesn't have the operator overloading or custom implicit casting that nullable types in VB.NET utilize. You really can't do it any better than variant.

An alternative is to choose a specific value and consistently treat that value as null. In .NET 1.0 days people used to use int.MinValue. I don't know what the VB6 equivalent is but I'm sure there's something. This works and is not nearly as bad as it sounds (but nullable types are better).

like image 103
Samuel Neff Avatar answered Oct 09 '22 10:10

Samuel Neff