Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2006

Tip: Looking for answers? Try searching our database.

newbie - How to test for null

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Laurel - 28 Sep 2006 22:16 GMT
How can I test to see if a string parameter is null?
This worked... or at least didn't give a compile error, but the debugger
seemed to show it skipping the assignment statement, and for sure, returnXML
was never set.  (Any help on that bit too?) ERPHostId is a string parameter
in the function, and this is the first executable statement in the function.
if (ERPHostId != null)

   returnXML = "<ERP></ERP>";

   return ReturnCode.FAILURE;

But this gave me a compile error:  C:\DEV QX
C#\6.4.1\QX.Data\QXMyInfo.cs(17): Cannot implicitly convert type 'string' to
'bool'

if (ERPHostId = null)
   returnXML = "<ERP></ERP>";

   return ReturnCode.FAILURE;
Laurel - 28 Sep 2006 22:26 GMT
Never mind.  I stumbled on ==.

Can someone point me to a simple-minded intro to C#?  I had searched MSDN
and Google for the answer to this, and I found a long list of c# operatiors
somewhere, but I simply couldn't understand the explanations - I'm not a
computer science guru - more of a self taught try-it person.

> How can I test to see if a string parameter is null?
> This worked... or at least didn't give a compile error, but the debugger
[quoted text clipped - 16 lines]
>
>    return ReturnCode.FAILURE;
Jonathan Roberts - 29 Sep 2006 05:29 GMT
> Never mind.  I stumbled on ==.
>
> Can someone point me to a simple-minded intro to C#?  I had searched MSDN
> and Google for the answer to this, and I found a long list of c# operatiors
> somewhere, but I simply couldn't understand the explanations - I'm not a
> computer science guru - more of a self taught try-it person.

Try these:

http://www.softsteel.co.uk/tutorials/cSharp/cIndex.html
http://www.publicjoe.f9.co.uk/csharp/csharp.html
chanmm - 29 Sep 2006 08:03 GMT
Have you download the C# spec from here?
http://msdn.microsoft.com/netframework/programming/clr/default.aspx

chanmm

> Never mind.  I stumbled on ==.
>
[quoted text clipped - 23 lines]
>>
>>    return ReturnCode.FAILURE;
Daniel - 29 Sep 2006 08:36 GMT
Not sure if you are still stuck but:

"if (ERPHostId != null)

   returnXML = "<ERP></ERP>";

   return ReturnCode.FAILURE;"

should be

if (!(ERPHostId == null))
{

   returnXML = "<ERP></ERP>";

   return ReturnCode.FAILURE;
}

i found != to not act as i would expect in c# so i do it the above way. If
ERPJostId is a string as you say then i presume you intialise this somewhere
like:

string ERPHostId = "";

if so then test it is still "" instead of checking for null, because unless
some method sets it to null it will just remain blank. so in that case:

if (!(ERPHostId.Equals("")))
{
   returnXML = "<ERP></ERP>";

   return ReturnCode.FAILURE;
}

returnXML will get set to that string value, i don't see why it wouldnt in
the above statement. However i noticed your code sample is missing the
braces {}, i presumed this was just how you posted it but your actual code
has it in? If your actual code doesn't then put them in as above if that is
that you want all that code to be within the if statement.

If there are other basics your stuck on feel free to email me direct or
reply on here.

> Never mind.  I stumbled on ==.
>
[quoted text clipped - 23 lines]
>>
>>    return ReturnCode.FAILURE;
Joanna Carter [TeamB] - 29 Sep 2006 09:50 GMT
| "if (ERPHostId != null)

| if (!(ERPHostId == null))

There is absolutely no difference between these two statements

| i found != to not act as i would expect in c# so i do it the above way. If
| ERPJostId is a string as you say then i presume you intialise this somewhere
| like:
|
| string ERPHostId = "";

You can also initialise a string like this :

{
 string ERPHostId = null;

but you could not do :

{
 string ERPHostId;

 if (ERPHostId == null)

...because ERPHostId is uninitialised.

| if so then test it is still "" instead of checking for null, because unless
| some method sets it to null it will just remain blank. so in that case:

In C#, strings can be either null or any empty string, so any test that
wants to know whether a string has any content must do both tests.

{
 if (!(ERPHostId == null))

...will only test that a string is null, it will not guarantee that the
string is empty.

{
 if (ERPHostId != null && ERPHostId != String.Empty)
   ...

Now, and only now have you safely got a string that has content.

Joanna

Signature

Joanna Carter [TeamB]
Consultant Software Engineer

Jon Skeet [C# MVP] - 29 Sep 2006 19:10 GMT
> Not sure if you are still stuck but:
>
[quoted text clipped - 15 lines]
>
> i found != to not act as i would expect in c# so i do it the above way.

No, != should work absolutely fine here - and is more easily readable.

Could you post a short but complete program which demonstrates != not
working properly?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

> if so then test it is still "" instead of checking for null, because unless
> some method sets it to null it will just remain blank. so in that case:
[quoted text clipped - 5 lines]
>     return ReturnCode.FAILURE;
> }

That's more easily readable as:

if (ERPHostId != "")

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Daniel - 29 Sep 2006 19:52 GMT
Sorry my bad, i was super tired when i wrote the reply, i use != in my code
lol.

>> Not sure if you are still stuck but:
>>
[quoted text clipped - 38 lines]
>
> if (ERPHostId != "")
Lukasz - 29 Sep 2006 10:35 GMT
> How can I test to see if a string parameter is null?

If it is .NET 2.0 this can be useful
String.IsNullOrEmpty

Signature

Pozdrawiam
Lukasz

Laurel - 30 Sep 2006 15:27 GMT
Thanks to you all.  Lot's of good info.  But a special thanks for this one,
as another mystery I had on my plate was why I got the message that "String"
didn't have an "IsNullOrEmpty," whereas I got the code I was trying strait
from MSDN.  But I'm not running 2.0.   Aha!!!

>> How can I test to see if a string parameter is null?
>>
> If it is .NET 2.0 this can be useful
> String.IsNullOrEmpty

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.