如何从按钮到方法识别变量?

时间:2015-06-14 08:34:27

标签: c# .net winforms

我试图计算特定文字中的字数和字符数。现在这一切都运行正常,但我想整理代码,以便我不必将你在newSummaryMethod()下看到的内容包含在每个按钮中,因此为什么我把它放在它自己的代码中方法。但是当我这样做时,它不会计算单词和字符。现在我知道原因是方法中string copyText = "";,因为如果我没有声明该字符串变量,那么我的按钮中会出现语法错误,copyText是没有宣布。

我的问题是关于如何让newSummaryMethod知道它需要首先与元音按钮中的copyText进行通信?我有另一个按钮,其中copyText的行为可能有点不同,所以我认为我需要按钮与方法进行通信。

 private void newSummaryMethod() {
     string copyText = "";

     /*Count number of lines in processed text,
extra line is always counted so -1 brings it to correct number*/
     int numLines = copyText.Split('\n').Length - 1;

     //seperate certain characters in order to find words
     char[] seperator = (" " + nl).ToCharArray();

     //number of words, characters and include extra line breaks variable
     int numberOfWords = copyText.Split(seperator, StringSplitOptions.RemoveEmptyEntries).Length;
     int numberOfChar = copyText.Length - numLines;

     //Unprocessed Summary
     newSummary = nl + "Word Count: " + numberOfWords + nl + "Characters Count: " + numberOfChar;
 }

 private void btnVowels_Click(object sender, EventArgs e) {
     //Strip vowels
     string vowels = "AaEeIiOoUu";
     string copyText = richTextBox1.Text;
     copyText = new string(copyText.Where(c => !vowels.Contains(c)).ToArray());

     newSummaryMethod();

     //Write into richTextBox2
     wholeText = richTextBox1.Text + oldSummary + copyText + newSummary;
     Write(Second_File, wholeText);
     richTextBox2.Text = wholeText;
 }

 private void btnAlpha_Click(object sender, EventArgs e) {

     //Remove non alpha characters
     string nonAlpha = @"[^A-Za-z ]+";
     string addSpace = "";
     string copyText = richTextBox1.Text;
     copyText = Regex.Replace(copyText, nonAlpha, addSpace);

     newSummaryMethod();

     //Write into richTextBox2
     wholeText = richTextBox1.Text + oldSummary + copyText + nl + newSummary;
     Write(Second_File, wholeText);
     richTextBox2.Text = wholeText;

 }

1 个答案:

答案 0 :(得分:1)

您可以将方法所需的数据作为参数传递给它

private void newSummaryMethod(string copyText) {...}

然后将其称为

newSummaryMethod(copyText);

另一种方法是在函数范围之外声明你的变量,这样两个函数都可以访问它。

string copyText = null;  //added


private void newSummaryMethod() {
     copyText = ""; //changed

     /*Count number of lines in processed text,
extra line is always counted so -1 brings it to correct number*/
     int numLines = copyText.Split('\n').Length - 1;

     //seperate certain characters in order to find words
     char[] seperator = (" " + nl).ToCharArray();

     //number of words, characters and include extra line breaks variable
     int numberOfWords = copyText.Split(seperator, StringSplitOptions.RemoveEmptyEntries).Length;
     int numberOfChar = copyText.Length - numLines;

     //Unprocessed Summary
     newSummary = nl + "Word Count: " + numberOfWords + nl + "Characters Count: " + numberOfChar;
 }

 private void btnVowels_Click(object sender, EventArgs e) {
     //Strip vowels
     string vowels = "AaEeIiOoUu";
     copyText = richTextBox1.Text; //changed
     copyText = new string(copyText.Where(c => !vowels.Contains(c)).ToArray());

     newSummaryMethod();

     //Write into richTextBox2
     wholeText = richTextBox1.Text + oldSummary + copyText + newSummary;
     Write(Second_File, wholeText);
     richTextBox2.Text = wholeText;
 }

 private void btnAlpha_Click(object sender, EventArgs e) {

     //Remove non alpha characters
     string nonAlpha = @"[^A-Za-z ]+";
     string addSpace = "";
     copyText = richTextBox1.Text; //changed
     copyText = Regex.Replace(copyText, nonAlpha, addSpace);

     newSummaryMethod();

     //Write into richTextBox2
     wholeText = richTextBox1.Text + oldSummary + copyText + nl + newSummary;
     Write(Second_File, wholeText);
     richTextBox2.Text = wholeText;

 }