Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2006

Tip: Looking for answers? Try searching our database.

nullable types

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mike P - 31 Aug 2006 11:50 GMT
What are nullable types used for?  The only use I can think of is where
I have a method like this where some values being passed to it may be
null :

public int AddUser(int? UserRegion, string UserName etc)

You might have an Add User page where some of the fields do not have a
value entered by the user, so you can account for this by making these
types nullable in the method above.  Is this the right usage of nullable
types?  And what are the other uses?
Andy Bates - 31 Aug 2006 12:03 GMT
Hi -

The use you state is valid.

They really come into their own when data needs to be able to identify when
it isn't holding a value. In SQL a statement could return a value or it
could return NULL. This doesn't cause too much problem for reference types
but obviously a value type needs a value (not unreasonable); this is where
the problem occurs.

You could assign an arbitrary NULL value (say 0) but it doesn't really model
NULL correctly as it's obviously in the range that the value holds.

Nullable types are created by the generic template struct Nullable and all
they essentially do is to maintain a boolean alongside the value indicating
whether it has been assigned to or not.

HTH

- Andy

> What are nullable types used for?  The only use I can think of is where
> I have a method like this where some values being passed to it may be
[quoted text clipped - 8 lines]
>
> *** Sent via Developersdex http://www.developersdex.com ***
Marc Gravell - 31 Aug 2006 12:04 GMT
That would seem to be a reasonable usage; more typical is representing data
from a database, where a null value has a specific meaning; otherwise you
need to rely on magic numbers such as "int.MinValue" or "-1" meaning "not
specified", or keep a separate "is <x> specified" flag. The nullable type
allows you to handle this directly, without risking messing up the database
by inserting a -1 by mistake.

Marc
Ignacio Machin ( .NET/ C# MVP ) - 31 Aug 2006 16:03 GMT
Hi,

Basically that is the kind of situation you use them for.

Personally I think that the more common example is a date that is not set.
This is a fairly common escenario when dealiing with DBs

Signature

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

> What are nullable types used for?  The only use I can think of is where
> I have a method like this where some values being passed to it may be
[quoted text clipped - 8 lines]
>
> *** Sent via Developersdex http://www.developersdex.com ***

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.