Home arrow C# arrow Page 3 - Introduction to Objects and Classes in C#, Part 2
C#

Introduction to Objects and Classes in C#, Part 2


After I wrote the article named Introduction to Objects and Classes in C#, I got a lot of e-mail messages asking me to create a series of articles about Objects and Classes. Actually this was a few months back (sorry for being late), but I'm here again with part two. In Part one, I explained the concepts behinds Objects and Classes but I didn't discuss why Object Oriented Programming (OOP) uses the Object and Class technique. Today, I will discuss the advantage of Objects and Classes with more details on how to understand your problems and develop your Objects for your solution.

Author Info:
By: Michael Youssef
Rating: 4 stars4 stars4 stars4 stars4 stars / 342
May 05, 2004
TABLE OF CONTENTS:
  1. · Introduction to Objects and Classes in C#, Part 2
  2. · Comments
  3. · What's a Scope?
  4. · Private Members Only?

print this article
SEARCH DEVARTICLES

Introduction to Objects and Classes in C#, Part 2 - What's a Scope?
(Page 3 of 4 )

Simply, the scope of a type (a variable, a method, or a class) is where you can use that type in your program. In other words, the scope defines the area of the program where that type can be accessible and referenced.

When you declare a variable inside a block of code (like a method or an if statement structure), it will have a local scope, and it will be called a local-variable. Local scope means that you can't refer to that variable outside that block of code. Consider the next example.


class Test

    
public void Test1()
    
{
        int x 
0;
            
// some code goes here that uses the x variable
    }
    
    
public void Test2()
    
{
        Console
.WriteLine(x);
    
}
}


 

Try to instantiate this class and you will get a compile-time error inside method Test2() telling you that the name x doesn't exist and that's because x is a local variable to the method Test1() and method Test2() doesn't know anything about it. So x has a local score to method Test1() only. Consider the next example.


class Test
{
 
    public 
void Test1()
    
{
        int x 
0;
 
        if(
== 0)
        
{
 
            
Console.WriteLine("x equal to 0");
        
}
    
}
}

 

Here, the method Test1() declares a local variable x (now x has a local-scope to the method). Try to instance this class and compile the code. It will work! Some beginners think that because I said that x has a local scope to method Test1() it will not be referenced from nested block (like the one we have here, the If statement) but that's not true because any nested block inside Test1() method can refer x because x is local for the method and its all blocks of code.

NOTE  There are 2 kinds of scopes: block scope (the one that we just finished), and a class scope (which we will talk about later in this article). Now, about the keywords private and public in the class person, you can use these access modifier keywords to define the scope of your variables, methods, or even your classes. There are other access modifiers but I will talk about them in a later article when I will explain the concepts of inheritance.

Instance variables declared using the access modifier keyword private will be accessible to the methods between the opening left brace "{" and the closing right brace "}" (which define the body of the class) only. In other words, when you declare an instance variable like in the following example:


public class Class2
{
    
private int x;
}
 
public class 
Class3
{
    void testing
()
    
{
        x 
== 100;
    
}
}

In this example, you will get a compile-time error telling you that you that the name X doesn't exist inside the Class3. Using the keyword private, you explicitly tell the compiler "Don't show this member to any other class." (So you are hiding it inside the class.)


blog comments powered by Disqus
C# ARTICLES

- Introduction to Objects and Classes in C#, P...
- Visual C#.NET, Part 1: Introduction to Progr...
- C# - An Introduction
- Hotmail Exposed: Access Hotmail using C#
- Razor Sharp C#
- Introduction to Objects and Classes in C#
- Making Your Code CLS Compliant
- Programming with MySQL and .NET Technologies
- Socket Programming in C# - Part II
- Socket Programming in C# - Part I
- Creational Patterns in C#
- Type Conversions
- Creating Custom Delegates and Events in C#
- Inheritance and Polymorphism
- Understanding Properties in C#

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials