Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.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/1/visual-studio-2012/2.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
Visual studio 2013 在不同的处理程序中更改变量_Visual Studio 2013 - Fatal编程技术网

Visual studio 2013 在不同的处理程序中更改变量

Visual studio 2013 在不同的处理程序中更改变量,visual-studio-2013,Visual Studio 2013,我正在创建变量和随机数,无法从脚本中的其他处理程序访问它们。有没有一种方法可以让我的所有事件处理程序都可以访问变量?表单处理程序中的整数无法在button1处理程序中编辑变量。请帮忙,谢谢 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;

我正在创建变量和随机数,无法从脚本中的其他处理程序访问它们。有没有一种方法可以让我的所有事件处理程序都可以访问变量?表单处理程序中的整数无法在button1处理程序中编辑变量。请帮忙,谢谢

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IqTest


{


public partial class Form1 : Form
{





    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)

    {

        Random rand = new Random();
        int ran = rand.Next(70, 100);
        string mystring = ran.ToString();
        label2.Text = mystring;
    }

    private void checkBox11_CheckedChanged(object sender, EventArgs e)
    {
        int ran = 0;
    }








}

}

您可以将整数声明为类级变量,然后两个事件处理程序都可以访问它

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IqTest


{


public partial class Form1 : Form
{
    private int ran;


    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)

    {

        Random rand = new Random();
        ran = rand.Next(70, 100);
        string mystring = ran.ToString();
        label2.Text = mystring;
    }

    private void checkBox11_CheckedChanged(object sender, EventArgs e)
    {
        ran = 0;
    }

}
}