null for int value

  • Thread starter Thread starter Barry400
  • Start date Start date
B

Barry400

Hi

i want to set the following
int xxx = null; // this is not allowed

i want to pass a null value to a method like this

setvalue("Hello world ", null); // to the following, it does not work,

private void setvalue(string s1, int iVal)
{

}

Barry
 
Barry400 said:
Hi

i want to set the following
int xxx = null; // this is not allowed

i want to pass a null value to a method like this

setvalue("Hello world ", null); // to the following, it does not work,

private void setvalue(string s1, int iVal)

Hi Barry,

you can't assign null to a "value" type such as int. You need to use the
nullable version, ie:
int? xxx = null;

lookup "nullable types" in the help file. ... I am not sure, but I think
that you would need to be on .Net v2.

Barry M
 
Barry400 said:
Hi

i want to set the following
int xxx = null; // this is not allowed

i want to pass a null value to a method like this

setvalue("Hello world ", null); // to the following, it does not work,

private void setvalue(string s1, int iVal)
{

}

Barry

In C# 2.0 you can use a nullable int:

private void setvalue(string s1, int? iVal)

You can pass either an integer value or null as the second argument.

In the method you use iVal.HasValue to determine if the nullable
variable has a value, and iVal.Value to get the value.
 
Back
Top