Friday, 31 July 2009

Beginning C# Lesson 4 - Variable Scope

When a software developer mentiones the word "scope" the develeoper is refering to "What can see this object, how can it see it, and whats its access and relation to other objects", this may sound daunting but i can assure you, its easy stuff :)

When a variable is declared where all of a class's members can view it, it is called a "Global" variable.

So far in my previous examples we have declared variables inside a method and used
it in that method. This is called a "Local"object. This means that only the method that declared it can view and use/edit the data. If i were to declare a variable called "myvar" in method Main then tried to assign it a value in a method called "MethodOne", it would show an error because it is trying to access a variable out side of its scope, as that variables scope is inside the main method.

If we declared that variable inside the "class" then we can view it in all methods that are inside the "scope" of that class.



You cannot declare or make a statement insid ethe namespace, as the namespace is just to prevent Class name clashing!

Well thats all for this tutorial, i cant explain much more as we havent covered other C# principles, but there will be a part two to it so stay tuned!

No comments:

Post a Comment