在方法中设置为 true 后返回 false 的布尔参数
Boolean parameter returning false after to be set to true in a method
我有一个表单调用另一个将执行操作的表单,如果该操作完成,我在父表单中作为参数放置的标志将在子表单中设置为 true,但如果操作是未完成,标志保持 false。
父表单中的代码:
bool flag = false;
new ChildForm(flag).ShowDialog();
if(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();
}
调试看到子窗体设置flag为true后,父窗体flag也为true,但是子窗体关闭后程序回执行父窗体代码,flag变回是假的。
发生了什么事?
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
}
有关通过 value/reference 传递变量的更多信息,请参阅 https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/passing-value-type-parameters。
它在父窗体中似乎变为 true
的事实可能只是在调试器中查看它的副作用。它实际上并没有将其更改为 true
.
如果您想从子表单中读取值,您需要使其可访问。
家长形式:
var c = new ChildForm();
c.ShowDialog();
if (c.Flag)
{
//some code that depends on that flag be true
}
子表:
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
}
}
}
我有一个表单调用另一个将执行操作的表单,如果该操作完成,我在父表单中作为参数放置的标志将在子表单中设置为 true,但如果操作是未完成,标志保持 false。
父表单中的代码:
bool flag = false;
new ChildForm(flag).ShowDialog();
if(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();
}
调试看到子窗体设置flag为true后,父窗体flag也为true,但是子窗体关闭后程序回执行父窗体代码,flag变回是假的。
发生了什么事?
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
}
有关通过 value/reference 传递变量的更多信息,请参阅 https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/passing-value-type-parameters。
它在父窗体中似乎变为 true
的事实可能只是在调试器中查看它的副作用。它实际上并没有将其更改为 true
.
如果您想从子表单中读取值,您需要使其可访问。
家长形式:
var c = new ChildForm();
c.ShowDialog();
if (c.Flag)
{
//some code that depends on that flag be true
}
子表:
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
}
}
}