日期:2014-05-17  浏览次数:20800 次

如何让一个方法使用其他方法中的变量?
本帖最后由 haoranhaoran 于 2013-02-02 06:41:51 编辑
如下 第二个方法无法访问第一个的变量 求解决方法 
在变量前面添加public static会出现一堆错误
显示txt文件随即的一行用 第一个方法中把文本每一行都存在一个数组中管理
第二个是想用来显示随即行用的 想避免重新读取文本每一行并且写入数组所以想分开放

            void openFileDialogFileOk(object sender, System.ComponentModel.CancelEventArgs e)
            {
                string fullPathname = openFileDialog.FileName;
                FileInfo src = new FileInfo(fullPathname);
                fileName.Text = src.Name;

                source.Text = "";
                TextReader reader = src.OpenText();
                string line = reader.ReadLine();
                int count = 0;
                while (line != null)
                {
                    line = reader.ReadLine();
                    count += 1;
                }
                string[] quest;
                quest = new string[count];
                int yes = 1;
                TextReader second = src.OpenText();
                while (yes != count)
                {
                    quest[yes] = second.ReadLine();
                    yes += 1;
                }
                Random ran = new Random();
                source.Text += quest[ran.Next(1, count)];
                reader.Close();
 &