c# - Accessing a string variable from one method to another -


    public static void introduction{          console.writeline("welcome");          string myname = "";          console.write("what's name ? : ");             myname = console.readline();         console.writeline("hey {0},welcome quiz! ", myname);          console.readline();      }     //quiz introduction  public static void fstquestion(){       //code      if(...)      {      console.writeline(" that's correct,{0} ! ",myname);      }       else         console.writeline(" keep trying,{0} ",myname); 

}

so want make quiz in c#,however,i want questions in different methods.as can see, want save variable user's name , use whenever needed.the problem don't know how call/use string method.any tip helps!thank you!

extract myname static field (in current code myname local variable , that's why beyond of scope in fstquestion()):

 private static string myname = "";   ...   public static void introduction{     console.writeline("welcome");      console.write("what's name ? : ");      myname = console.readline();      console.writeline("hey {0},welcome quiz! ", myname);      console.readline();  }   ...   public static void fstquestion() {    //code     if (...) {      console.writeline(" that's correct,{0} ! ",myname);    }    else      console.writeline(" keep trying,{0} ",myname);    ...  

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -