newbie - How to test for null

  • Thread starter Thread starter Laurel
  • Start date Start date
L

Laurel

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;
 
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.
 
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.
 
"Daniel" <[email protected]> a écrit dans le message de (e-mail address removed)...

| "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
 
Daniel said:
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.

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:

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

return ReturnCode.FAILURE;
}

That's more easily readable as:

if (ERPHostId != "")
 
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!!!
 

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

Back
Top