PC Review


Reply
Thread Tools Rate Thread

creating a nullable integer structure

 
 
giant food
Guest
Posts: n/a
 
      21st Oct 2003
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
 
Reply With Quote
 
 
 
 
Fergus Cooney
Guest
Posts: n/a
 
      21st Oct 2003
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/pr...o/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


 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      21st Oct 2003
* (E-Mail 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.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      21st Oct 2003
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

"giant food" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between nullable class and nullable<> structure Raj Microsoft C# .NET 31 19th Oct 2009 11:01 PM
RE: Nullable(Of Integer) question BobRoyAce Microsoft VB .NET 5 20th Mar 2008 10:32 PM
Problem with Nullable( Of Integer) bound to combobox Flomo Togba Kwele Microsoft Dot NET Framework Forms 7 12th Feb 2007 04:34 AM
Creating a nullable type using Activator =?Utf-8?B?RGVyZWtH?= Microsoft Dot NET Framework 3 22nd May 2006 02:09 PM
Pointer to integer into structure... =?Utf-8?B?Y2xvcmVudHNvbg==?= Microsoft Dot NET Framework 1 22nd Feb 2005 02:35 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:28 AM.