G+_Mike Tierce Posted June 2, 2014 Share Posted June 2, 2014 Hey guys! I'm a little late to the party but I just finished up Module 1 on C# and wanted to post what I learned... I have a quick question though. Why is that once I've captured the console.readline() and saved it as a string whatever = consoleread~~ that my functions can't seem to recognize the variable? https://github.com/miketierce/CodeTesting/blob/master/Program.cs Link to comment Share on other sites More sharing options...
G+_Joe Maruschek Posted June 3, 2014 Share Posted June 3, 2014 You are running into what is called "variable scope". In other words, those variables you created in your main routine are only visible in certain places. In the case of C#, they are only visible within the curly braces of the main() routine. All of your other sub-routines were defined outside of the the main() routine, so they can't see the variables declared inside of main(). The usual way to solve this problem is to define a parameter on your sub-routines, and then pass in the needed data as an argument when you call the sub-routine. For example, you could change UserNameFont() to UserNameFont(string username). Now you can pass in a string to your subroutine and it can use that data locally. Make sense? Link to comment Share on other sites More sharing options...
Recommended Posts