473,320 Members | 1,904 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,320 software developers and data experts.

nullable types

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?

*** Sent via Developersdex http://www.developersdex.com ***
Aug 31 '06 #1
3 2557
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 <xspecified" flag. The nullable type
allows you to handle this directly, without risking messing up the database
by inserting a -1 by mistake.

Marc
Aug 31 '06 #2
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

"Mike P" <mi*******@gmail.comwrote in message
news:Ot**************@TK2MSFTNGP03.phx.gbl...
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?

*** Sent via Developersdex http://www.developersdex.com ***

Aug 31 '06 #3
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
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Mike P" <mi*******@gmail.comwrote in message
news:Ot**************@TK2MSFTNGP03.phx.gbl...
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?

*** Sent via Developersdex http://www.developersdex.com ***

Aug 31 '06 #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...
12
by: Steven Livingstone | last post by:
I've just blogged some stuff on Nullable types in net 2.0. http://stevenr2.blogspot.com/2006/01/nullable-types-and-null-coalescing.html Question however as to why you can't simply get an implcit...
6
by: Steven Livingstone | last post by:
Bit of advice here folks. I am creating a default constructor that just initializes a new instance of my object and a secon constructor that takes an ID and loads an object with data from the...
8
by: shawnk | last post by:
Given several nullable boolean flags; bool? l_flg_01 = true; bool? l_flg_02 = false; bool? l_flg_03 = true; bool? l_result_flg = null; I would have liked...
0
by: Larry Lard | last post by:
There seems to be something a bit lacking in the way the dataset designer thing deals (or rather doesn't) with nullable fields in VS2005. Maybe it's cos I'm using VB2005 Express (which is variously...
1
by: Joe Bloggs | last post by:
Hi, Can someone please kindly show me how to determine if a type (read value type) is Nullable. MSDN has this KB: How to: Identify a Nullable Type (C# Programming Guide)...
8
by: Sam Kong | last post by:
Hello, I want to define a generic class which should accept only nullable types or reference types. What's the best way to costrain it? --------- class MyClass<T>{ ...
3
by: MobileBoy36 | last post by:
Hi all, Nullable types were announced as new handy stuff in .NET 2.0 But it seems like the datareader doesn't support nullable types. You have still to check for "IsDbNull". So, are Nullable...
6
by: Tony Johansson | last post by:
Hello! I'm reading in a book called Visual C# 2005. In the chapter about Generics there ia a section about Nullable types. Here is the text that isn't complete true I think. It says: "You...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.