【问题标题】:How to ref a variable value inside a button_click method如何在 button_click 方法中引用变量值
【发布时间】:2014-12-14 06:35:44
【问题描述】:

我有一个类可以构造一种特殊的消息框。 在该类中,参数之一是我在构造函数中引用的变量 但是,我需要从构造函数中识别出该变量,更具体地说,当我单击按钮时。

我查看了方法的行为,并注意到为了让方法从变量中识别值,我必须像这样引用它。

static void SetString1(ref string value)
{
if (value == "cat") // Test parameter value
{
    Console.WriteLine("Is cat");
}
value = "dog"; // Assign parameter to new value
}

我也想做同样的事情,但是在按钮单击方法中,但是如果我尝试引用变量“variavelcaixa”,它会给我“buttonRight_click 匹配委托 System.eventhandler 没有重载”。这是什么意思,我应该如何成功引用变量?

private void buttonRight_Click(object sender, System.EventArgs e, ref int variavelcaixa)
    {
        if (checkBox1.Checked == true)
        { variavelcaixa = 1; }
        else { variavelcaixa = 0; }
    }

编辑: 我在特殊消息框类中的代码如下:

     public partial class BetterDialog : Form
   {
    public int variavelcaixa;
 static public DialogResult ShowDialog(string title, string largeHeading, string smallExplanation,
        string leftButton, string rightButton, Image iconSet, ref int variavelcaixa)
    {
        using (BetterDialog dialog = new BetterDialog(title, largeHeading, smallExplanation, leftButton,
            rightButton, iconSet, ref variavelcaixa))
        {
            DialogResult result = dialog.ShowDialog();
            return result;
        }
    }

    /// <summary>
    /// The private constructor. This is only called by the static method ShowDialog.
    /// </summary>
    private BetterDialog(string title, string largeHeading, string smallExplanation,
        string leftButton, string rightButton, Image iconSet, ref int variavelcaixa)
    {
        this.Font = SystemFonts.MessageBoxFont;
        this.ForeColor = SystemColors.WindowText;

        InitializeComponent();
  //A bunch of graphic design
    }

在构造函数之外还有按钮点击方法

    private void buttonRight_Click(object sender, System.EventArgs e)
    {
        if (checkBox1.Checked == true)
        { variavelcaixa = 1; }
        else { variavelcaixa = 0; }
    }

在主类中,我只需添加 ref variavelcaixa,并在消息框对象上附加一个特定变量

MsgBoxCheck.MessageBox dlg = new MsgBoxCheck.MessageBox();
                string icone = "C:\\warning.png";
                DialogResult result = BetterDialog.ShowDialog("Alert",
      "main message in message box",
      "some secondary message",
      "", "Ok", System.Drawing.Bitmap.FromFile(icone), ref Variables.checkbox53naomostrarnovamente);

【问题讨论】:

  • 即使您作为ref 传入,您实际上也没有分配给变量。在参数和变量中具有相同名称不会自动赋值。
  • @shree.pat18 那么如何链接它们呢?
  • 赋值给ShowDialog中的变量!
  • 但是 ShowDialog 已经有 ref int variavelcaixa 作为参数
  • @shree.pat18 你能提供更详细的信息吗?没听懂,抱歉:\

标签: c# methods buttonclick ref


【解决方案1】:

按钮单击事件处理程序必须具有函数签名(object sender, System.EventArgs e)MSDN Reference。这就是为什么当您尝试让您的处理程序接受第三个参数时,会出现错误。

解决此问题的一种方法是使变量variavelcaixa 可供处理程序方法访问。您可以通过在类中将其声明为全局变量来做到这一点。然后,您可以在构造函数以及事件处理程序方法中分配给它。请注意,一旦执行此操作,就不应将 ref int variavelcaixa 传递给事件处理程序方法。

基本上,一旦您调用showDialog 并传入ref 参数,您需要将其分配给方法体中的类变量variavelcaixaref Variables.checkbox53naomostrarnovamente 将引用 checkbox53naomostrarnovamente,而不是您的变量。因此,将您的代码更改为:

static public DialogResult ShowDialog(string title, string largeHeading, string smallExplanation, string leftButton, string rightButton, Image iconSet, ref int p_variavelcaixa)
{
    using (BetterDialog dialog = new BetterDialog(title, largeHeading, smallExplanation, leftButton,
        rightButton, iconSet, ref p_variavelcaixa))
    {
        variavelcaixa = p_variavelcaixa;
        DialogResult result = dialog.ShowDialog();
        return result;
    }
}

请注意,我已将您的 ref 参数重命名为 p_variavelcaixa 以帮助消除混淆。您可能还想看看this demo 以了解变量 - 参数的事情。

【讨论】:

  • 这是最简单的方法,我尝试这样做,但即使这样,他也无法识别变量值,尽管代码运行时没有错误。我正在尝试将该变量添加为以下代码中的额外参数dotnetperls.com/customized-dialog
  • 那个链接上有很多代码,你指的是哪一个?此外,您可能希望发布您使用我建议的方法编写的代码,以便我们了解它有什么问题。
  • 我在一个版本中添加了代码,并且在消息框类中,variavelcaixa 当然是公开的:)
  • aw 是的,但现在它给我带来了另一个错误,variavelcaixa = p_variavelcaixa;“非静态字段、方法或属性 'DotNetPerls.BetterDialog.variavelcaixa' 需要对象引用”跨度>
  • 你需要以object.variable 的身份访问它,除非你将其设为静态变量。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多