473,395 Members | 1,692 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

creating a nullable integer structure

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
Nov 20 '05 #1
3 1813
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
Nov 20 '05 #2
* di**********@yahoo.com (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>
Nov 20 '05 #3
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" <di**********@yahoo.com> wrote in message
news:a6**************************@posting.google.c om... 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

Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: John Wood | last post by:
I was just looking at an article about using nullable value types. (value types that can effectively have no value and not be set). The syntax is to append a question-mark to the value type in...
9
by: Mark Rae | last post by:
Hi, I posted a couple of days ago about the possibility of "simulating" in ..NET1.1 the nullable datatypes available in .NET2.0 - I'm nearly there, but require a bit more guidance. Basically,...
5
by: Narshe | last post by:
How can I check if a type is nullable? If a type is created like bool? = null;, then how can you tell if the type is nullable as opposed to not? You have to cast from bool to bool? and such, so...
3
by: Nemisis | last post by:
Hi everyone, i have been stressing over this for the past hour or so, and just cannot figure it out, sorry if i am being stupid, but it is a friday! lol I have the following Private _ID as...
9
by: MLM450 | last post by:
Is there a way to get the address of the underlying value of a nullable variable without copying the value? I tried the following, but the compiler doesn't like it. I need to do this because I am...
3
by: Goofy | last post by:
The 2.0 Framework has a new type called System.Nullable. This allows something to Not exist, basically you can pass 'Nothing' to a method whose parameters as System.Nullable. Im looking at table...
15
by: scparker | last post by:
I have yet to find a satisfactory solution to this problem. It involves VB.NET 2.0 and datetime issues. I have a form that asks for a Date to be submitted in dd/mm/yyyy format. When this is...
4
by: Aamir Mahmood | last post by:
Hi I am unable to access the HasValue and Value properties in an Nullable object. int? i = 9; object value = i; if ((value as Nullable).HasValue) { ... }
4
by: James | last post by:
hello, can we have a nullable type field in the struct ? struct Employee { nullable<stringfirstName =null; } we can't initialize a field in a struct so is there any possibility that we can...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.