Home > Archive > C# > May 2005 > arg expects an int, i want to pass a null
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
arg expects an int, i want to pass a null
|
|
| thomasamillergoogle@yahoo.com 2005-05-14, 4:04 pm |
| I have a function:
private void UpdatePersons(int PersonId, int RoleId)
{
if(RoleId == null)
//do database stuff setting RoleId column to DbNull
else
//do something else
}
So, there are situations where I would like to call this function like:
UpdatePersons(22,null)
I have hundreds of these functions, they are a DAL generated by the MS
Application Block. So I would like to find some kind of way to do this.
I fear I amy have to pass -1 instead of null and then handle it in my
dal?
| |
| Bruce Wood 2005-05-14, 4:04 pm |
| For now, yes. The best you can do is choose an "illegal" value and have
that represent null.
In v2.0 of the Framework you can declare the parameters as int?:
private void UpdatePersons(int? PersonId, int? RoleId)
and they will be able to be null. v2.0 is currently in Beta2 (available
via free download from MSDN) and is due out in production later this
year (no word as to when, but my crystal-ball prediction is at PDC
2005, in September).
| |
| MasterGaurav 2005-05-14, 4:04 pm |
| Use something like "-1". A "reserved" value that should be taken up
otherwise.
null is a reserved value not applicable in valid scenarios, similary
this value. For example, a value of '-1' may mean that you want to pass
a 'null'.
--
Cheers,
Gaurav Vaish
http://mastergaurav.blogspot.com
http://mastergaurav.org
| |
|
| > For now, yes. The best you can do is choose an "illegal" value and have
> that represent null.
>
> In v2.0 of the Framework you can declare the parameters as int?:
>
> private void UpdatePersons(int? PersonId, int? RoleId)
>
> and they will be able to be null. v2.0 is currently in Beta2 (available
> via free download from MSDN) and is due out in production later this
> year (no word as to when, but my crystal-ball prediction is at PDC
> 2005, in September).
>
If you are not using beta2 you can use a special value to indicate a null
value or create a wrapper class for integer. I wish they would just drop the
concept of any intrinsic types being value types. The whole nullable idea is
nice, but it does not really fix problems since nullable types loose some of
the functionality you would want. Unless you are doing huge amounts of
calculations wrapper classes on all the value types will not really be
noticable to the average user. (Here is wishing that Integer, Double, Float,
etc. were wrapper classes and not value types at least)
|
|
|
|
|