C# 通过构造函数访问主类变量

C# 通过构造函数访问主类变量,c#,constructor,C#,Constructor,我使用一个单独的类来构造一个带有“不再显示”复选框的消息框 一切运行正常,但我想为每个messagebox对象添加一个不同的变量 所以我选择了原来的课程,加入了一个论点 static public DialogResult ShowDialog(string title, string largeHeading, string smallExplanation, string leftButton, string rightButton, Image iconSet, int v

我使用一个单独的类来构造一个带有“不再显示”复选框的消息框

一切运行正常,但我想为每个messagebox对象添加一个不同的变量

所以我选择了原来的课程,加入了一个论点

static public DialogResult ShowDialog(string title, string largeHeading, string smallExplanation,
        string leftButton, string rightButton, Image iconSet, int variavelcaixa)
其中,“int variavelcaixa”是int类型的变量,每个messagebox对象都将具有该类型的变量

例如,我将在主类中创建一个消息框,如下所示:

MsgBoxCheck.MessageBox dlg = new MsgBoxCheck.MessageBox();
                string icone = "C:\\warning.png";
                DialogResult result = BetterDialog.ShowDialog("Alert",
      "Warning message 1",
      "some consequences in a string",
      "Do action button", "Cancel button", System.Drawing.Bitmap.FromFile(icone), variableofthismessagebox);
其中,每个messagebox对象上的IsMessageBox变量将不同


问题:当选中复选框时,如何通过二级类中的VariableCaixa更改variableofthismessagebox的值?

我想您正在查找关键字


ref
将使
variavelcaixa
指向主类中的变量
variableofthismessagebox


更多信息:

wow,但是如果是这样的话,我们为什么不对其他参数使用ref,比如字符串和图像呢?数据类型(
string
s,
int
s等)是按值传递的,默认情况下不是引用。(其中作为引用类型,例如
Image
,其属性通过引用传递)这意味着如果传递值为
0
int
,并且在方法中对其进行修改,则调用代码中不会更改。使用
ref
关键字告诉它指向
int
的实际引用。通常情况下,您要么使用引用类型,要么使用数据类型,但不需要在调用的方法中修改它。嗯,不是这样,我认为这是因为我试图更改复选框单击事件中变量的值。因为它是一个与构造函数分离的事件,所以我必须在类的开头将“variavelcaixa”声明为public,否则它在事件中无法识别它。我做错什么了吗?我用的正是下面这节课你的假设是正确的,我明天早上会写点东西。
static public DialogResult ShowDialog(string title, string largeHeading, string smallExplanation, string leftButton, string rightButton, Image iconSet, ref int variavelcaixa)
DialogResult result = BetterDialog.ShowDialog("Alert",
  "Warning message 1",
  "some consequences in a string",
  "Do action button", "Cancel button", System.Drawing.Bitmap.FromFile(icone),
   ref variableofthismessagebox);