[英]Why is my C# if statement code not working?
I can't figure out why this If statement doesn't work. 我不知道为什么这个If语句不起作用。
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. 它应该做的是在这种情况下,我正在制作一个XWM到Wav转换器,将textBox4中的文件扩展名替换为相反的文件扩展名。 so IF textbox2 and textBox4 contain the same file extension it will change the one in textBox4 to the other file type.
因此,如果textbox2和textBox4包含相同的文件扩展名,它将把textBox4中的扩展名更改为另一种文件类型。
Why isn't it working. 为什么不起作用。
PS: I'm a noob on C# so explain it as best as you can to a noob PS:我是C#的菜鸟,所以请尽量向菜鸟解释
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. 一种知道的方法是查看函数的(替换)原型,它返回一个字符串,因此这很可能意味着该实例即textbox4.text不会被更改。
You're calling Replace
on a string, but then not doing anything with the result. 您正在对字符串调用
Replace
,但是对结果不执行任何操作。 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). 字符串在C#中是不可变的-听起来好像他们在更改字符串的任何方法实际上只是返回对新字符串的引用(如果不需要更改,则可能返回对旧字符串的引用)。 So calling
Replace
(or similar methods) and then ignoring the result is always pointless. 因此,调用
Replace
(或类似方法)然后忽略结果总是毫无意义的。
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: 顺便说一句,我还将摆脱
== true
,并且很可能提取对文本框的所有读取访问权限:
// 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:- 我想您在以下几点进行检查时做错了:
textBox4.Text = textBox4.Text.Replace(".xwm", ".wav");
textBox4.Text = textBox4.Text.Replace(“。xwm”,“ .wav”);
Well if statement by default takes your first statement and will allow you to enter if it is true. 好吧,如果默认情况下,if语句采用您的第一条语句,并且允许您输入是否为真。
so final code would be as below :- 所以最终的代码如下:
if (textBox2.Text.Contains(".xwm") && textBox4.Text.Contains(".xwm"))
如果(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: 扩展Jon Skeet的答案:
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. 在这种情况下,您实际上不应该使用
String.Replace
始终最好使用System.IO
命名空间提供的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.