creating a nullable integer structure

  • Thread starter Thread starter giant food
  • Start date Start date
G

giant food

since I can't specify an integer to be null (i.e. undefined), I'd like
to create a structure that provides the same functionality, like:

Public Structure NullableInt
Private i As Integer
Private bHasValue As Boolean 'false if null
End Structure

so that works okay, but I'd like users to be able to use integers in
places where a NullableInt is called for. For example, suppose there
is a function SaveValue(x as NullableInt). I'd like to allow this sort
of call:

SaveValue(7)

Is there a function I can get to write to get .NET to cast the 7 into
a NullableInt? And if I wrote the following:

SaveValue(System.DBNull.Value)

....I'd like that to convert to NullableInt as well. Is this possible
in .NET?

Is there a better method (or already-existing datatype) that I'm
missing?
Thanks!

-Frank
 
Hi Frank,

"Giant Food" - Lol, that's a good 'un!

You'd have to have a few overloads for your SaveValue. But you are
thinking, I imagine, of other situations where you haven't the option of
coding in that way, eg assignments.

Implicit type casting is not available in VB.NET as yet. Nor is operator
overloading where you can add an Integer and a NullableInt. You have the
option of waiting for the version that includes it.

From
http://msdn.microsoft.com/vstudio/productinfo/roadmap.aspx

<quote> Visual Basic Whidbey ...
Finally, for the more advanced Visual Basic developer, language enhancements
include support for operator overloading, unsigned data types, inline
XML-based code documentation, and partial types. In addition, developers using
Visual Basic will have access to a type-safe, high-performance, compile
time-verified version of generics that promote code reuse across a variety of
data types.
</quote>

... or using a <real> language like C# (lol)

Regards,
Fergus
 
* (e-mail address removed) (giant food) scripsit:
Public Structure NullableInt
Private i As Integer
Private bHasValue As Boolean 'false if null
End Structure

so that works okay, but I'd like users to be able to use integers in
places where a NullableInt is called for. For example, suppose there
is a function SaveValue(x as NullableInt). I'd like to allow this sort
of call:

SaveValue(7)

Is there a function I can get to write to get .NET to cast the 7 into
a NullableInt? And if I wrote the following:

SaveValue(System.DBNull.Value)

...I'd like that to convert to NullableInt as well. Is this possible
in .NET?

You can create a constructor with a parameter for 'NullableInt' that
takes the value and sets it.
 
Frank,
Is there a better method (or already-existing datatype) that I'm
missing?
..NET actually already defines such a structure.

Check out the types in the System.Data.SqlTypes namespace.

They were created to help support the System.Data.SqlClient namespace,
however you are free to use them with your own design (or instead of your
own design as the case may be).
so that works okay, but I'd like users to be able to use integers in
places where a NullableInt is called for. For example, suppose there
is a function SaveValue(x as NullableInt). I'd like to allow this sort
of call:

SaveValue(7)
As Fergus suggested Overload the SaveValue method.

Public Sub SaveValue(ByVal x as NullableInt)
' do work here
End Sub

Public Sub SaveValue(ByVal x As Integer)
SaveValue(New NullableInt(x))
End Sub

Public Sub SaveValue(ByVal x As DBNull)
SaveValue(New NullableInt(x))
End Sub

Where you have defined NullableInt with:
Public Structure NullableInt
Private i As Integer
Private bHasValue As Boolean 'false if null

Public Sub New(ByVal i As Integer)
End Sub

Public Sub New(ByVal null As DBNull)
End Sub
End Structure

Hope this helps
Jay
 
Back
Top