I am fairly new to C# and have encounted only one big problem thusfar. How does one create a variable that can be accessed/viewed/modified from other blocks of code? For instance, if I wanted to have variable x defined in one block of code as 1, but then wished to either check if variable x was one, or modify variable x, from a completely seperate block of code, how would I do that? I am familiar with a few methods of indirectly changing the variable, but they are generally complicated and too troublesome to do on a regular basis, and do not allow for other uses of the variable. Another example would be if I define variable x in one class, can I use variable x in an if statement of some sort in a later class. Any help would be appreciated, thanks.
C# Public Variable (accessable from other blocks)?
Well, first, you must realize that C# is an object-based programming language - not a procedural one. In most OOP languages, 'classes' are created, and methods (sometimes called gets and sets, or properties) are created to allow other classes to change the values of variables (attributes)in other classes. It seems as though you are looking for an easy way to do this, and all I could suggest - and may the programming Gods forgive me for what I am about to say - would be to make one giant class, with a bunch of methods, and declare all of your variables as 'Public' at the class level. Now, this is absolutely NOT RECOMMENDED, as it violates all that is good and holy about OOP. If you really want to develop effectively in C#, Java, or any other 3GL, you really need to get comfortable with encapsulation, and the methods of modifiying data between classes.
Good luck to you : )
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment