TechTalkz.com Logo Ask the Experts!

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Programing Languages > C#(C Sharp)

Check null

C#(C Sharp)

 
 
Thread Tools Display Modes
Unread 12-11-2007, 05:02 AM   #1
RP
Guest
 
Posts: n/a
Check null

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#.

 
Unread 12-11-2007, 05:02 AM   #2
Arne Vajh°j
Guest
 
Posts: n/a
Re: Check null

RP wrote:
> 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

 
Unread 12-11-2007, 01:02 PM   #3
colin
Guest
 
Posts: n/a
Re: Check null


"Arne Vajh°j" <arne@vajhoej.dk> wrote in message
news:4737d9a6$0$90276$14726298@news.sunsite.dk...
> RP wrote:
>> 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
>


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

Colin =^.^=


 
Unread 12-11-2007, 03:04 PM   #4
Marc Gravell
Guest
 
Posts: n/a
Re: Check null

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

 
Unread 12-11-2007, 05:04 PM   #5
Ben Voigt [C++ MVP]
Guest
 
Posts: n/a
Re: Check null


"RP" <rpk.general***********> wrote in message
news:1194842051.797349.241640@e34g2000pro.googlegr oups.com...
>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?


 
 

Thread Tools
Display Modes



< Home - Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT. The time now is 01:19 AM.


vBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2013, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional