简体   繁体   English

Visual Studio C#if / else语法错误

[英]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 因此,您不能将其用作变量的名称
  • The other problem is that you use = 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_Bitcoinradio 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.

相关问题 Visual Studio 17 C#一切都是语法错误 - Visual Studio 17 c# everything is a syntax error Visual Studio正在将我的C#语法检查为Visual Basic - Visual studio is syntax checking my C# as Visual Basic 如何在 Visual Studio(社区)的 C# 中打开语法错误突出显示? - How do I turn on the syntax error highlighting in C# in Visual Studio (Community)? C# Visual Studio 2019 红色波浪线表示语法错误在打开“打开本地文件夹”文件时不可见 - C# Visual Studio 2019 red squigglies for syntax error not visible when “Open a local folder” file is opened intellisense无法找出Visual Studio和C#中的“内部”可访问性级别语法错误 - intellisense can't figure out a 'internal' accessibility level syntax error of in visual studio & C# Visual Studio-C#项目以C ++语法显示 - Visual Studio - C# project appearing with C++ Syntax 在Visual Studio C#中使用datetimepicker的正确语法代码是什么? - What is the proper syntax code in using datetimepicker in visual studio c#? 有关Nullable的Visual Studio 2010和C#语法 <T> ? - Visual Studio 2010 and C# Syntax about Nullable<T>? .NET C#Visual Studio 2008错误 - .Net c# visual studio 2008 error 错误的PowerShell C#Visual Studio中 - error powershell c# visual studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM