c# - How a variable can be recognized from button to method? -
i'm trying count number of words , characters in specific text. works fine want tidy code don't have include the see under newsummarymethod()
every button, hence why have placed in it's own method. when this, doesn't count words , characters. know reason string copytext = "";
in method, that's because if don't declare string variable, syntax errors in button copytext
not declared.
my question how can newsummarymethod
know needs communication copytext
in first vowels button? have button copytext
may behave bit differently think need button communicate method.
private void newsummarymethod() { string copytext = ""; /*count number of lines in processed text, line counted -1 brings correct number*/ int numlines = copytext.split('\n').length - 1; //seperate characters in order find words char[] seperator = (" " + nl).tochararray(); //number of words, characters , include 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 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 richtextbox2 wholetext = richtextbox1.text + oldsummary + copytext + nl + newsummary; write(second_file, wholetext); richtextbox2.text = wholetext; }
you can pass data method needs argument
private void newsummarymethod(string copytext) {...}
and call like
newsummarymethod(copytext);
another way declaring variable outside function scope both functions have access it.
string copytext = null; //added private void newsummarymethod() { copytext = ""; //changed /*count number of lines in processed text, line counted -1 brings correct number*/ int numlines = copytext.split('\n').length - 1; //seperate characters in order find words char[] seperator = (" " + nl).tochararray(); //number of words, characters , include 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 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 richtextbox2 wholetext = richtextbox1.text + oldsummary + copytext + nl + newsummary; write(second_file, wholetext); richtextbox2.text = wholetext; }
Comments
Post a Comment