以下代码中有错误 [英] Having Errors in the following code
本文介绍了以下代码中有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Fun_with_If_else
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if(checkBox1.checked==true)
{
if(button1=checked)
{
MessageBox.Show("this is not checked");
}
else
{
label1.BackColor=Color.Green;
}
}
}
}
推荐答案
if(button1=checked)
将始终引发异常.应该是if(button1.checked)
.
既然我已经如此迅速地回答了这个问题,我意识到这看起来很像是考试或面试问题.糟糕...
if(button1=checked)
will always throw an exception. It should beif(button1.checked)
.
Now that I have so quickly answered this question, I realize that this looks a lot like an exam or interview question. Oops...
您的拼写错误"在"checkBox1.checked == true"中选中为已选中"是您的错误来源之一.
如果"button1"是WinForms Button控件:WinForms Button不具有已检查"属性,并且它永远不能与任何属性相等",除非类似:"if(buttton1 == null)".在那种情况下,我们将对象(按钮)与可能的空"或未定义"状态进行比较,这实际上是对象与对象的比较.
您需要获得一本有关C#的好书,并从基础知识入手,包括确保您了解C#区分大小写,并了解对象之间以及对象属性之间的区别.
Your mis-spelling ''Checked as ''checked in "checkBox1.checked==true" is one of your source of errors.
If "button1" is a WinForms Button Control: a WinForms Button has no ''Checked property, and it could never be "equal" to any property, except in a case like: "if (buttton1 == null)." In that case we compare an object (Button) to a possible "empty," or "undefined" state, which really is an object to object comparison.
You need to get a good book on C#, and start with the basics, including making sure you understand that C# is case-sensitive, and learn what the differences between objects, and Properties of objects are.
这篇关于以下代码中有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文