Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法从其他类设置值_C#_Class_Variables - Fatal编程技术网

C# 无法从其他类设置值

C# 无法从其他类设置值,c#,class,variables,C#,Class,Variables,我试图将LabelStatus的文本设置为类中的消息,但它不起作用。 这是我的密码: 类别: public bool openConnection() { SetStatus("Connecting to " + Server); //Mysql code } private void SetStatus(string msg) { Form1 form = new Form1(); form.SetStatus(msg); } 表格1: pub

我试图将LabelStatus的文本设置为类中的消息,但它不起作用。 这是我的密码:

类别:

public bool openConnection()
{
    SetStatus("Connecting to " + Server);       
    //Mysql code
}

private void SetStatus(string msg)
{
    Form1 form = new Form1();
    form.SetStatus(msg);
}
表格1:

public void SetStatus(string status)
{
    labelStatus.Text = _status;
}

我对C#(php guy)相当陌生,我一辈子都搞不清楚我做错了什么

看起来你是在设置成员变量,而不是函数的参数

 //try something like this
this._status = status;
this.labelStatus.Text = this._status;

设置
labelStatus.Text
时,您没有使用传递给
SetStatus(string)
的参数进行设置。您似乎无意中使用了数据成员。

尝试调用表单上的或方法

private void SetStatus(string msg) 
{ 
    Form1 form = new Form1(); 
    form.SetStatus(msg); 
    form.ShowDialog(this);
} 

从您的代码中,我认为您的类正在更改表单标签的状态标签。若要更改表单标签文本,您需要已打开表单的对象。在类中为窗体定义变量

public class ConnectionCheck
{
  private Form myForm;

   public void   ConnectionCheck(Form form)
  {
    myForm = form;
  }

  public bool openConnection()
  {
    SetStatus("Connecting to " + Server);       
    //Mysql code
  }

  private void SetStatus(string msg)
  {
     //Call method to change label text
      myForm .SetStatus(msg);
  }

}
在连接时传递form1对象检查从from1 codebehind(form1.cs)创建的对象

同时,将_状态更改为参数变量

public void SetStatus(string status)
{
    labelStatus.Text = status;
}

看看这些名字:试着让它们相同,明白吗

labelStatus.Text = **status**;

public void SetStatus(string status)
中,它是
status
,但在它的主体中,您使用带有下划线的_status…这是一个输入错误吗?您还需要在form@BenShepherd你能解释一下,你说“没用”是什么意思吗?你在期待什么?那么会发生什么呢?这个
形式
变量是什么?你用它做过什么吗?看起来您只是在初始化它并将其留给垃圾收集。我希望
LabelStatus.Text
更改
状态的任何内容,但我没有看到任何更改,因为您没有在当前表单上设置它。您正在创建一个新表单,设置状态,然后将其留待处理。@BenShepherd,您看到表单本身了吗?您需要使用
show
ShowDialog
方法来显示它
labelStatus.Text = **status**;