简体   繁体   中英

Why is my C# if statement code not working?

I can't figure out why this If statement doesn't work.

if (textBox2.Text.Contains(".xwm") && textBox4.Text.Contains(".xwm") == true) 
{
    textBox4.Text.Replace(".xwm", ".wav");
}
else if (textBox2.Text.Contains(".wav") && textBox4.Text.Contains(".wav") == true) 
{
    textBox4.Text.Replace(".wav", ".xwm");
}

What its supposed to do is replace the file extension in textBox4 with the opposite one in this case I'm making a XWM to Wav Converter. so IF textbox2 and textBox4 contain the same file extension it will change the one in textBox4 to the other file type.

Why isn't it working.

PS: I'm a noob on C# so explain it as best as you can to a noob

strings are immutable, meaning, the way you have to change them is by reassigning them.

textBox4.Text = textBox4.Text.Replace(".wav", ".xwm");

a way to know is looking at the function's (replace) prototype, it returns a string, so this probably means that the instance, ie: textbox4.text is not going to be changed.

You're calling Replace on a string, but then not doing anything with the result. Strings are immutable in C# - any methods which sound like they might be changing the string actually just return a reference to a new string (or potentially a reference to the old one if no change was required). So calling Replace (or similar methods) and then ignoring the result is always pointless.

I suspect you want:

textBox4.Text = textBox4.Text.Replace(".xwm", ".wav");

As an aside, I'd also get rid of the == true , and quite possibly extract all the read accesses to the textboxes:

// Rename these as appropriate - and rename the textBox* variables so the names
// explain the purpose.
string source = textBox2.Text;
string target = textBox4.Text;
if (source.Contains(".xwm") && target.Contains(".xwm"))
{
    textBox4.Text = target.Replace(".xwm", ".wav");
}
else if (source.Contains(".wav") && target.Contains(".wav"))
{
    textBox4.Text = target.Replace(".wav", ".xvm");
}

(I suspect there are even better ways of expressing what you're trying to achieve, but at the moment we don't know what that is...)

I guess you are doing it wrong for that check below points:-

  1. .Replace method returns string so you need to write it as below :-

textBox4.Text = textBox4.Text.Replace(".xwm", ".wav");

  1. Well if statement by default takes your first statement and will allow you to enter if it is true.

    so final code would be as below :-

if (textBox2.Text.Contains(".xwm") && textBox4.Text.Contains(".xwm"))

  { textBox4.Text = textBox4.Text.Replace(".xwm", ".wav"); } else if (textBox2.Text.Contains(".wav") && textBox4.Text.Contains(".wav")) { textBox4.Text = textBox4.Text.Replace(".wav", ".xwm"); } 

Expanding on Jon Skeet's answer:

string source = textBox2.Text;
string target = textBox4.Text;

Func<string, string, bool> func = (path, ext) => {
    return ext.Equals(Path.GetExtension(path), StringComparison.InvariantCultureIgnoreCase);
};

if (func(source, ".xwm") && func(target, ".xwm"))
{
    textBox4.Text = Path.ChangeExtension(target, ".wav");
}
else if (func(source, ".wav") && func(target, ".wav"))
{
    textBox4.Text = Path.ChangeExtension(target, ".xvm");
}

Given the scenario, you really shouldn't be using String.Replace - it's always best to use the methods provided by the System.IO namespace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM