954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

String is != "" but at the same time == ""

I have a little spoky problem here. I really cant understand what it is.
I am reading a file that is completely empty. But still the second MessageBox is showing this string:

"22"

I cant wrap my head around why if( line != "" ) is executing and no character is shown inbetween the "22" ?

String^ line = "";
 WebClient^ Client = gcnew WebClient();

 Client->Credentials = gcnew NetworkCredential("username", "password");
 Stream^ strm = Client->OpenRead("file1.txt");
 StreamReader^ sr = gcnew StreamReader(strm);    

 line = sr->ReadLine();
 sr->Close();
 strm->Close();


 if( line == "" )
 {
	 MessageBox::Show("1" + line + "1");
 }
 if( line != "" )
 {
	 MessageBox::Show("2" + line + "2");
 }
Lukezzz
Posting Whiz in Training
268 posts since Mar 2008
Reputation Points: 10
Solved Threads: 1
 

I'm not familiar enough with .NET to know the exact operator(s) you need, but as I understand things, "String^ line" is a pointer to a string, it is not an actual string. Try to de-reference the pointer when you do your comparison.

That's just my 2-cents. Take it how you like.

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

Thanks, okay, but I am quite shure that this is a correct declaration for a string. Anyway this is the way I always have done it:

String^ line = "";
Lukezzz
Posting Whiz in Training
268 posts since Mar 2008
Reputation Points: 10
Solved Threads: 1
 

This happens because StreamReader::ReadLine returns nullptr when the end of stream is reached (which is the case when reading an empty file).

That means that on line 8 of your code line is now nullptr , and not the initial empty string. Since they do not compare equal to each other, the second if statement is entered, and because nullptr does not have a string representation, nothing is printed between the two 2's.

To observe the correct behavior, change the last part to:

if ( String::IsNullOrEmpty( line ) )
{
	MessageBox::Show("1" + line + "1");
}
else
{
	MessageBox::Show("2" + line + "2");
}
gashtio
Light Poster
29 posts since Dec 2009
Reputation Points: 35
Solved Threads: 14
 

Like I said, I'm not that familiar with .NET. All I can do is mention some general concepts and see if they spark something for you. AFAIK, that is a correct declaration...

I was actually referring to Lines 13 and 17.

[edit]
Oops, overlapped with a much better answer...

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

Thank you, now I understand why it didn“t work, so that did solve that problem

Thanks alot for help!

This happens because StreamReader::ReadLine returns nullptr when the end of stream is reached (which is the case when reading an empty file).

That means that on line 8 of your code line is now nullptr , and not the initial empty string. Since they do not compare equal to each other, the second if statement is entered, and because nullptr does not have a string representation, nothing is printed between the two 2's.

To observe the correct behavior, change the last part to:

if ( String::IsNullOrEmpty( line ) )
{
	MessageBox::Show("1" + line + "1");
}
else
{
	MessageBox::Show("2" + line + "2");
}
Lukezzz
Posting Whiz in Training
268 posts since Mar 2008
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You