Library tutorials & articles

The Quick & Dirty .NET Guide to C#/VB OOP

Let's Up The Variables' Property Value A Bit

Huh? Sorry, I always try for clever wordplay :-) The usage of variables as seen is direct and to the point. But what if we had someone trying to build a house with color's that simply won't do? Well aside from our field assignment, I'll now demonstrate fields kicked up a notch - these being properties that allow you to perform conditional testing to prevent any faulty or unwanted values from being passed in.

Within our properties you deal with two accessors - Get and Set , that work in a read-write fashion. Get retrieves and references any calls from within, for which Set assigns our value when called.

In addition, our summary method now employs our properties as an alternative to our simple variables.

[C#]
public class House {
  //Exposed variables
  private string ShowSize;
  private string HseBuilder;
  private string HseColor;
  //Private properties
  public string ShowBuilder {
    get {return HseBuilder;}
    set {HseBuilder = value;}
  }
  public string ShowColor{
    get {
      if (HseColor == "red"){
        return HseColor + ". But I'll change it to white.";
      }else{
        return HseColor;
      }
    set {HseColor = value;}
  }

  public string Summary() {
    return ShowBuilder + " is building a new house that's " + ShowSize + " and is painting it " + ShowColor;
  }
}
[VB]
Public Class House
  Private ShowSize As string
  Private HseBuilder As string
  Private HseColor As String

  Public Property ShowBuilder As String
    Get
      return HseBuilder
    End Get
    Set
      HseBuilder = value
    End Set
  End Property
  Public Property ShowColor As String
    Get
      If (HseColor = "red") Then
        Return HseColor & ". But I'll change it to white."
      Else
        Return HseColor
      End If
    End Get
    Set
      HseColor = value
    End Set
  End Property

  Public Function Summary() As String
    Return ShowBuilder & " is building a new house that's " & ShowSize & " and is painting it " & ShowColor
  End Function

End Class

And in our .aspx page we can add, and by now you should've gotten the VB conversion idea as well, the code below:

House MyOtherHouse = new House();
MyOtherHouse.HseBuilder = "Dude";
MyOtherHouse.ShowSize = "looks ok";
MyOtherHouse.HseColor = "red";
Response.Write (MyOtherHouse.Summary());

So after all's said and done our results will look like this:

The first and last values of our summary method are utilized from our private properties ShowBuilder(), and ShowColor() that incorporates our conditional statement checking that if someone tries to paint our house red, we'll change that to white. The second one is simply from our plain variable - ShowSize . Therefore, we can clearly observe how properties are like super sized variables. Both are accessed the same way, but properties have a little more power over what you permit to take place.

Notice how we use words like public or private in front of our methods, properties or variables? These are what are generally known as access modifiers . In our object, recall how our HseBuilder, HseSize, and HseColor variables are public. With access modifiers we implicitly determine for our object's user what members they will be allowed to be modify or gain access to, their scope in other words.

In our example, the properties that retrieve these assigned public variables and analyze their content are private , and accessible to the class itself and cannot be accessed externally. The true nature of OOP insists on such, that the whole intent is defined by their proper usage, determining their external access allowances.

There are five allowed modifiers or accessibility levels in C# and they consist of:

  • public - Full access is allowed to this
  • private - Accessible from within the calls that contains it
  • internal - Can only be accessed from the current DLL or assembly. Friend in VB.
  • protected - Like private above, and could be access in a class derived from the one it's in
  • protected internal - Can only be accessed from any derived class, itself and like internal - the current DLL it resides in. In VB this would be Protected Friend .

Note: Any time you don't specify any modifiers to a variable, it will default to private . Likewise, on any properties or methods , its modifiers default to public .

Moreover, as far are modifiers are concerned, others exist, for both variable fields and methods, that won't be looked into here, like extern, volatile, sealed, NotInheritable or NotOverridable , etc. In light of the article's objective, .NET's exhaustive documentation would suffice better in clarifying any terms not delved into here. Still, upon reading this guide, any other OOP terms and their supposed uses won't be nearly as challenging to figure out as they may have once been.

Comments

  1. 25 Jul 2006 at 13:14

    hello,


    I am very impressed by your tutorial as it finally allowed me to grasp the syntax behind OOP programming with .net.
    Only what I did not understand is how and where do I complile the .cs to a dll? I cannot do it on the server. Do I do it on my local computer and then upload?


    http://www.developerfusion.co.uk/show/4341/5/

  2. 22 Jun 2006 at 06:29

    Honestly I believe that the basic idea of the OOP was really great, but to be able to use it one really has to have the head as a water melon. There is too much theory, too many therms and the class theory really feels like puting each part of the program into a separate box and then figuring out how to drill a way between them. I started to learn C++ OOP at least 20 time and after a couple of weeks I did not even had an idea what it is about (note: I am not a proffesional programmer)...

    I believe that: "The use of a programming language should be as simple as a pie and the algorithm should be the part where people spend the most of their time..."

    When I do C# programming I do all public and it works great for me. Simply keep it simple!!!

    Is there any web page or book where I could find how to do Non-OOP C# programming?

    Sincerely,

    Gabor Gorcsos

     

     

     

  3. 20 May 2005 at 23:21

    Though from personal experience I'd say 99.99% of the time, you'd want private member variables and public properties...

  4. 22 Apr 2005 at 18:00

    Hi Ehx,


    That's true, and it's funny that in all my other articles I always write all private variables with public properties. i.e. - Building a Full-Featured Custom DataGrid Control. It's just one of those overlooked things. Oh well.

  5. 22 Apr 2005 at 17:26

    After reading your article, http://www.developerfusion.com/show/4341/6/
    I got realy confused!!


    from what I know from the book below, I declare private property, then declare public (get , set )
    What you presented in your article is you declared public property,then  made the (get,set) private)


    I have put this simple comparison code , so please advice your point, What is the logic behind reversing the modifier(public to private and vis versa).
    thanks
    Ehx
     
        // what is in the book (Begining Asp.Net Database using C# p 321)
           private string Country;
           public string Country
           {
               get{return _Country;}
               set{
    Country= value;}
           }


                  // what is in web article ( in your article)
           public string Name;
           private string _Name
           {
               get{return Name;}
               set{Name= value;}
           }

  6. 01 Jan 1999 at 00:00

    This thread is for discussions of The Quick & Dirty .NET Guide to C#/VB OOP.

Leave a comment

Sign in or Join us (it's free).

Dimitrios Markatos Dimitrios, or Jimmy as his friends call him, is a .NET developer/architect who specializes in Microsoft Technologies for creating high-performance and scalable data-driven enterprise Web and deskto...

Related podcasts

  • A Practical Look at Silverlight 2 Part 1

    Now that Silverlight 2 is at the Olympics and making a big splash, we wanted to explore this fascinating technology more. Microsoft Silverlight 2 is a cross-browser, cross-platform, and cross-device plug-in for delivering the next generation of .NET based media experiences and rich interactive ap...

Events coming up

  • Dec 9

    GL.net Group Meeting - December 2009

    Gloucester, United Kingdom

    The beginning of this year holiday season will belong to mocks. Ronnie and Stephen will take us for a tour around exciting world of unit testing.

Want to stay in touch with what's going on? Follow us on twitter!