0

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

Waqas Raja
  • 10,802
  • 4
  • 33
  • 38
Brennan
  • 17
  • 9
  • Replace method returns the changed string. Assign it back to the `TextBox.Text`. – Mat J Sep 26 '14 at 05:30
  • 1
    Unrelated: you could clean this up a lot (aesthetically) by assigning `var txt2 = textBox2.Text` and `var txt4 = textBox4.Text` and using those. Also, there's no need for `== true` in an if statement condition. – Asad Saeeduddin Sep 26 '14 at 05:30
  • FWIW: *Avoid* using `x == false` or `x == true` or the negated variations - especially in cases where such is used inconsistently! This is because `x == true` is semantically equivalent to `x`, where `x` is a boolean. – user2864740 Sep 26 '14 at 05:36
  • string in .net is immutable type so that you have to assing new string what was return by Replace to your old variable (textBox4.Text in you case) have a look http://stackoverflow.com/questions/2365272/why-net-string-is-immutable – Vladimir Shmidt Sep 26 '14 at 06:36

4 Answers4

9

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, i.e: textbox4.text is not going to be changed.

Tal
  • 700
  • 4
  • 11
  • 1
    I think talking about "the way you have to change them" is confusing - it's conflating the ideas of "the contents of the string object" (which can't be changed) and "which object the `textBox4.Text` property (in this case)" refers to. – Jon Skeet Sep 26 '14 at 05:35
5

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...)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

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");
    }
Neel
  • 11,625
  • 3
  • 43
  • 61
0

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.

Ed Courtenay
  • 520
  • 5
  • 14