Reputation: 4334
I'm trying to count the number of words and characters in a specific text. Now this all works fine but I want to tidy up code so that I don't have to include the the you see under newSummaryMethod()
into every button, hence why I have placed it in it's own method. But when I do this, it doesn't count the words and characters. Now I know the reason is that string copyText = "";
in the method, that's because if I don't declare that string variable, then I will get syntax errors in my button that copyText
is not declared.
My question is really about how can I get the newSummaryMethod
to know that it needs to communication with the copyText
in first the vowels button? I have another button where copyText
may behave a bit differently so I think I need the button to communicate with the method.
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;
}
Upvotes: 0
Views: 39
Reputation: 20764
you can pass the data the method needs to it as an argument
private void newSummaryMethod(string copyText) {...}
and then call it like
newSummaryMethod(copyText);
another way is declaring your variable outside the function scope so both functions will have access to it.
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;
}
Upvotes: 1