-4

Well I was trying to assign global variable to read values from Check boxes and radio buttons but the values don't update when the selections are changed ! 我哪里做错了?这是代码:

private void chkInMut_Checked(object sender, RoutedEventArgs e)
    {
        GlobalVar.Mutate = 1;
    }

    private void chkShwCal_Checked(object sender, RoutedEventArgs e)
    {
        GlobalVar.ShowCal = 1;
    }

    private void chkOutSol_Checked(object sender, RoutedEventArgs e)
    {
        GlobalVar.OutCal = 1;
    }
}

public static class GlobalVar
{
    static int _MaxMin, _MutVal, _CalShow, _CalOut;

    /// <summary>
    /// Access routine for global variable.
    /// </summary>

    public static int Extrema
    {
        get
        {
            return _MaxMin;
        }
        set
        {
            _MaxMin = value;
        }
    }

    public static int Mutate
    {
        get
        {
            return _MutVal;
        }
        set
        {
            _MutVal = value;
        }

    }

    public static int ShowCal
    {
        get
        {
            return _CalShow;
        }
        set
        {
            _CalShow = value;
        }
    }

    public static int OutCal
    {
        get
        {
            return _CalOut;
        }
        set
        {
            _CalOut = value;
        }
    }
}

当我尝试使用此测试语句打印数字时,返回的值是意外的:

        maxMin = GlobalVar.Extrema;
        calShow = GlobalVar.ShowCal;
        calOut = GlobalVar.OutCal;
        IsMutble = GlobalVar.Mutate;
        txtOutput.Text += Convert.ToString("\nMaxima Minima"+maxMin+"\n"+"Show Cal : "+calShow+"\n"+"Output Cal :"+calOut+"\n"+"Mutate : "+IsMutble+"\n---------\n");

当我选中/取消选中这些框时,这些值不会按应有的方式更新。我哪里出错了?

编辑:通过添加未选中的参数来解决。

4

2 回答 2

1

可能您应该像这样编写事件处理程序

private void chkInMut_Checked(object sender, RoutedEventArgs e)
{
    GlobalVar.Mutate = (chkInMut.IsChecked ? 1 : 0);
}

等等 .....

于 2012-12-27T08:45:34.117 回答
0

我认为问题出在您的公共静态属性上。例如试试这个:

public static int Extrema
{
    get
    {
        return GlobalVar._MaxMin;
    }
    set
    {
        GlobalVar._MaxMin = value;
    }
}

并对所有其他属性执行相同操作。

编辑: 你为什么要使用这种结构?您可以将静态类设置为:

public static class GlobalVar
{
   public static int Extrema;
   public static int Mutate;
     public static int ShowCal;
}
于 2012-12-27T08:34:42.653 回答