[英]Visual Studio C# if / else syntax error
I have a problem with my if/else code :/ 我的if / else代码有问题:/
private void btn_Convert_Click(object sender, EventArgs e)
{
if (rbtn_Bitcoin = checked)
{
webBrowser1.Navigate("http://preev.com/btc/sek");
}
else
{
webBrowser1.Navigate("http://preev.com/ltc/sek");
}
}
if (rbtn_Bitcoin = checked)
after this statement I get three errors, "Syntax error, '(' expected" and two ") expected" 如果在此语句之后(rbtn_Bitcoin = checked)
,我将得到三个错误,“语法错误,'('预期“和两个”)预期“
Your code has two errors: 您的代码有两个错误:
checked
is a reserved keyword. checked
是保留关键字。 Therefore you cannot use it as a name of a variable 因此,您不能将其用作变量的名称 =
which is the assignment operator and not the ==
which is the operator for equality comparison 另一个问题是您使用=
作为赋值运算符,而不是==
这是相等比较的运算符 =
is assignment operator. =
是赋值运算符。 ==
is equality operator. ==
是相等运算符。
I assume you want to get your radio button is checked or not, you can use RadioButton.Checked
Property instead. 我假设您要检查是否选中单选按钮,可以改用RadioButton.Checked
属性 。
Gets or sets a value indicating whether the control is checked. 获取或设置一个值,该值指示是否选中控件。
if (rbtn_Bitcoin.Checked)
{
webBrowser1.Navigate("http://preev.com/btc/sek");
}
else
{
webBrowser1.Navigate("http://preev.com/ltc/sek");
}
Double the =
and use checked
property: 双击=
并使用checked
属性:
private void btn_Convert_Click(object sender, EventArgs e)
{
if (rbtn_Bitcoin.Checked == true) <<-- == instead of =
{
webBrowser1.Navigate("http://preev.com/btc/sek");
}
else
{
webBrowser1.Navigate("http://preev.com/ltc/sek");
}
}
Also checked
is a reserved word, you can't use that this way. 同时checked
是保留字,您不能通过这种方式使用它。 if you want to check whether the radio button is checked, use the Checked
property. 如果要检查单选按钮是否已选中,请使用Checked
属性。
You can also omit the == true
part: 您也可以省略== true
部分:
if (rbtn_Bitcoin.Checked)
{
}
Assuming rbtn_Bitcoin is a radio button: 假设rbtn_Bitcoin是一个单选按钮:
private void btn_Convert_Click(object sender, EventArgs e)
{
if (rbtn_Bitcoin.Checked)
{
webBrowser1.Navigate("http://preev.com/btc/sek");
}
else
{
webBrowser1.Navigate("http://preev.com/ltc/sek");
}
}
The question is already answered. 问题已经回答。
But if rbtn_Bitcoin
is a radio button
then you can use the following code directly without any comparison using ==
sign 但是,如果rbtn_Bitcoin
是radio button
则可以直接使用以下代码,而无需使用==
符号进行任何比较
private void btn_Convert_Click(object sender, EventArgs e)
{
if (rbtn_Bitcoin.IsChecked)
{
webBrowser1.Navigate("http://preev.com/btc/sek");
}
else
{
webBrowser1.Navigate("http://preev.com/ltc/sek");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.