Why does this IF statement fail?
Posted
by
ChosenOne
on Stack Overflow
See other posts from Stack Overflow
or by ChosenOne
Published on 2013-07-03T16:49:28Z
Indexed on
2013/07/03
17:05 UTC
Read the original article
Hit count: 335
If variable path
is empty, and editor.Text
is not empty, the SaveFileDialog should be displayed.
Now, why on earth is this damn thing failing???
I have tried this with many different variations of code with the same result: FAIL:
if(path.Length >= 1) // path contains a path. Save changes instead of creating NEW file.
{
File.WriteAllText(path, content);
}
else
{
// no path defined. Create new file and write to it.
using(SaveFileDialog saver = new SaveFileDialog())
{
if(saver.ShowDialog() == DialogButtons.OK)
{
File.WriteAllText(saver.Filename, content);
}
}
}
At the top of code file I have:
path = String.Empty;
So why the heck it this failing every single time, even after trying all of the below variations?
if(path.Length > 1) // path contains a path. Save changes instead of creating NEW file.
{
File.WriteAllText(path, content);
}
else
{
// no path defined. Create new file and write to it.
using(SaveFileDialog saver = new SaveFileDialog())
{
if(saver.ShowDialog() == DialogButtons.OK)
{
File.WriteAllText(saver.Filename, content);
}
}
}
and
if(String.IsNullOrEmpty(path)) // path contains a path. Save changes instead of creating NEW file.
{
File.WriteAllText(path, content);
}
else
{
// no path defined. Create new file and write to it.
using(SaveFileDialog saver = new SaveFileDialog())
{
if(saver.ShowDialog() == DialogButtons.OK)
{
File.WriteAllText(saver.Filename, content);
}
}
}
and
if(String.IsNullOrWhiteSpace(path)) // path contains a path. Save changes instead of creating NEW file.
{
File.WriteAllText(path, content);
}
else
{
// no path defined. Create new file and write to it.
using(SaveFileDialog saver = new SaveFileDialog())
{
if(saver.ShowDialog() == DialogButtons.OK)
{
File.WriteAllText(saver.Filename, content);
}
}
}
This is making me very angry. How could this fail?
Setting a break point reveals that path
is definitely null
/""
.
© Stack Overflow or respective owner