Check null

R

RP

I have a member variable of a class of type "int", and while assigning
value to it in a constructor, I want to check whether it is a null. I
am using following code:

if (NewMember.BPLNo == null)
NewMember.BPLNo = 0;

But, Visual Studio 2005 is giving warning that variables with type
'int' cannot be 'null'. How to check this?

I also want to know is there any functions like IsNumeric, IsString
etc. in C#.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

RP said:
I have a member variable of a class of type "int", and while assigning
value to it in a constructor, I want to check whether it is a null. I
am using following code:

if (NewMember.BPLNo == null)
NewMember.BPLNo = 0;

But, Visual Studio 2005 is giving warning that variables with type
'int' cannot be 'null'. How to check this?

I also want to know is there any functions like IsNumeric, IsString
etc. in C#.

No need to check, because int can not be null.

int? can be null if you need the capability.

Arne
 
C

colin

Arne Vajhøj said:
No need to check, because int can not be null.

int? can be null if you need the capability.

Arne

if(NewMember.BPLNo.GetType()==typeof(String)
...
if(NewMember.BPLNo.GetType().IsValueType) or IsClass etc ..
...

Colin =^.^=
 
M

Marc Gravell

C> if(NewMember.BPLNo.GetType().IsValueType) or IsClass etc ..

If you start down the reflection road you need to be very careful;
int? (Nullable<int>) is a value-type, and is nullable depending on
your definition of nullable. Also you'd need to watch for the BPLNo
being null - but in general, the reflection approach is only needed if
you don't know what your data is - either because of "object", or
perhaps because of generics. In the case of a property this seems
overkill. Especially for an int property where there is no real
question of nullability.

Marc
 
B

Ben Voigt [C++ MVP]

RP said:
I have a member variable of a class of type "int", and while assigning
value to it in a constructor, I want to check whether it is a null. I
am using following code:

All memory is zero-filled before the constructor runs, this means that
references become "null" and numeric variables become zero.
if (NewMember.BPLNo == null)
NewMember.BPLNo = 0;

But, Visual Studio 2005 is giving warning that variables with type
'int' cannot be 'null'. How to check this?

I also want to know is there any functions like IsNumeric, IsString
etc. in C#.

Perhaps you want the TryParse function of int/long/float/double/decimal for
validation of strings that are supposed to be numbers?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top