c# - How to create a function to hold one block of code and then execute? -


i have 2 buttons contain own functionality (which have not included in code snippet below not relevant), contain same block of text (which shown in code snippet below). question beginner in c#, there way can write code once , use function shall call placed in buttons instead?

code snippet:

        private void btnalpha_click(object sender, eventargs e)         {  //replace non alpha code go here…  /*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 btnreplace_click(object sender, eventargs e) {  //replace code go here…  /*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;          } 

update: full code here: http://paste.ofcode.org/h3zvtamkyanmqfsqy2xnny

in c# can enclose reusable code in methods (as suggested in comments). if there parts of code behave differently again can encapsulate them separate methods. below code that's repeated in each handler in mymethod. btnreplace specific code in myreplace , btnalpha specific code in myalpha:

private void btnreplace_click(object sender, eventargs e) {     myreplace();     mymethod(); }  private void btnalpha_click(object sender, eventargs e) {     myalpha();     mymethod(); }  private void myreplace() {     // replace code }  private void myalpha() {     // alfa code }  private void mymethod() {     //replace code go here…      /*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; } 

if need sort of communication between methods 1 option return value first method , pass second one.

alternatively can parameterize main method (if true execute alfa part else execute replace part) , execute parameter saying part of code execute. if there many possible alternatives producing separate method each alternative makes more sense.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -