01 April 2012

Introduction to OOP #Part4

  Scopes  -  As supposed and planned I will continue with defining the usage of classes in OOP which is very important point to understand the flexibility and useability logic into OOP.  At the last post  is described in short points what 'modifiers' are and for what they'll be used for, now while keeping simplicity I'll try to explain and write about 'Scopes',  so scopes are something like 'access area of declared variables', say for example we've declared an variable into one method named 'myVariable' this variable we can use only inside our method, that means if we have one variable in one method we can't access that variable in the other method, Let's see it:


        void Method1()
        {
            int myVariable = 0;
        }

        void Method2()
        {
            // here we will get an error:
            //The name 'myVariable' does not exist in the current context
            myVariable = 1;
        }

At the code above, we can't access the 'myVariable' variable from Method2, because it isn't in the current context (scope), in order to access this variable inside two methods we have to declare it at the same level as our methods (for example declaring above Method1), you can read more about scopes here.

A small summary:
  • Declarations
  • Classes
  • Variables
  • Properties
  • Methods
  • Functions
  • Modifiers
  • Scopes
     Those points above were just touched (described) in very short way,  just to go further in the coming posts with new 'stuff'.

What's next?
I'm sorry about my short time, for next post I'll try to cover 'Inheritance and Implementation' inside classes to make it more OOP, Feel free for comment and share content or ask questions.
    

No comments:

Post a Comment