Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.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# 在方法中设置为true后返回false的布尔参数_C#_Parameters_Boolean_Flags - Fatal编程技术网

C# 在方法中设置为true后返回false的布尔参数

C# 在方法中设置为true后返回false的布尔参数,c#,parameters,boolean,flags,C#,Parameters,Boolean,Flags,我有一个表单,它调用另一个表单,该表单将执行一个操作,如果该操作完成,我在父表单中作为参数放置的标志将在子表单中设置为true,但如果该操作未完成,该标志将保持false 父窗体中的代码: bool flag = false; new ChildForm(flag).ShowDialog(); if(flag) { //some code that depends on that flag be true } var c = new ChildForm(); c.ShowDialog(); i

我有一个表单,它调用另一个表单,该表单将执行一个操作,如果该操作完成,我在父表单中作为参数放置的标志将在子表单中设置为true,但如果该操作未完成,该标志将保持false

父窗体中的代码:

bool flag = false;
new ChildForm(flag).ShowDialog();
if(flag)
{
//some code that depends on that flag be true
}
var c = new ChildForm();
c.ShowDialog();
if (c.Flag)
{
    //some code that depends on that flag be true
}
子窗体中的代码:

bool flag;
public ChildForm(bool flag)
{
InitializeComponent();
this.flag = flag;
}
private SomeMethod()
{
//some code
flag = true;
this.Close();
}
public bool Flag { get; private set; }
public ChildForm()
{
    InitializeComponent();
}
private SomeMethod()
{
    //some code
    Flag = true;
    this.Close();
}
调试它时,我看到,在子窗体中将标志设置为true之后,父窗体中的标志也为true,但是在关闭子窗体并重新编程以执行父窗体代码之后,标志变回false

发生了什么?

flag变量通过值传递给ChildForm的构造函数,并分配给私有变量(也称为flag)。这意味着对私有变量的任何更改都不会影响原始变量

要解决这个问题,您需要将ChildForm变量声明为public,并在原始方法中使用它

public boolean flag;
public ChildForm(boolean flag)
{
  InitializeComponent();
  this.flag = flag;
}
private SomeMethod()
{
  //some code
  flag = true;
  this.Close();
}
并按如下方式更改原始方法

boolean flag = false;
var form = new ChildForm(flag);
form.ShowDialog();
if(form.flag)
{
//some code that depends on that flag be true
}

有关按值/引用传递变量的更多信息,请参阅。

在父窗体中它似乎变为
true
,这可能只是在调试器中查看它的一个副作用。它实际上并没有将其更改为
true

如果要从子窗体读取值,则需要使其可访问

父窗体:

bool flag = false;
new ChildForm(flag).ShowDialog();
if(flag)
{
//some code that depends on that flag be true
}
var c = new ChildForm();
c.ShowDialog();
if (c.Flag)
{
    //some code that depends on that flag be true
}
子表单:

bool flag;
public ChildForm(bool flag)
{
InitializeComponent();
this.flag = flag;
}
private SomeMethod()
{
//some code
flag = true;
this.Close();
}
public bool Flag { get; private set; }
public ChildForm()
{
    InitializeComponent();
}
private SomeMethod()
{
    //some code
    Flag = true;
    this.Close();
}

您可以这样做:

public class ChildForm {
    public ChildForm(bool flag) {
       InitializeComponent();
       this.Flag = flag;
    }

    private SomeMethod() {
        //some code
        this.Flag = true;
        this.Close();
    }
    public bool Flag {get;}
}

public class ParentForm {
   public void Foo() {
      bool flag = false;
      var child = new ChildForm(flag);
      child.ShowDialog();
      if(child.Flag) {
          //some code that depends on that flag be true
      }
   }
}

在这个代码示例中,如果(flag)是entering@BennoDual怎样?子窗体中的方法将flag设置为true。它将子窗体中的flag设置为true-这对父窗体中的变量没有影响。@BennoDual正如我所说,调试器向我显示父窗体中的flag为true,这就是我不理解的原因。调试中的副作用欺骗了我。谢谢,正如我所说,调试向我表明父标志是正确的,这就是我不理解的原因。你解决了我的问题,谢谢。我说,调试告诉我父标志是正确的,这就是我不理解的原因。这很有效,谢谢