Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 48,845 times

Contents

Related Categories

The Quick & Dirty .NET Guide to C#/VB OOP - A Class in Objects

DMarko1

A Class in Objects

Simply put, a class is a created object . In more detail, a class is a reference type (that which stores some type of information) that encapsulates (hides or encloses) and or modularizes (making for flexible, varied use) any amount of methods (functions or subroutines), fields, variables or properties or constructors (object creating function) that classifies data and performs some kind of functionality. Thus, the focal point of OOP is to classify all common functionality and members within a main class that when utilized or instanced in your application, it becomes an object . Object creation is achieved by using the new keyword in your code, as we'll see.

The examples we'll mostly focus will revolve around the House class and its key traits/characteristics. Now if we had a house class and it contained its pertinent house characteristics ( fields or variables) it would look something like this. So go ahead and place your preferred language code section in a text file. Next save it as House.cs if using C# or House.vb for VB. This is your source file that you'll compile in the next section.

[C#]
public class House {
  // Its members
  // This is known as initializing a field - see the "Quick Structs" section further down
  // public string HseColor = "gray";
  public string HseBuilder;
  public string HseSize;
  public string HseColor;
  public string Summary() {
    return HseBuilder + " is building a new house that's " + HseSize + " and is painting it " + HseColor;
  }

}

[VB]
Public Class House
  Public HseBuilder As String
  Public HseSize As String
  Public HseColor As String
  Public Function Summary() As String
    Return HseBuilder &" is building a new house that's " & HseSize &" and is painting it " & HseColor
  End Function
End Class

The bits of information we include above in our House blueprint is who the builder is, the house's size and its color. Further enhanced by a summary entailing overall what's included and taking place within our House class.

Therefore, even though this is one template describing one house, it could easily represent and be utilized for any number of houses since each of its public properties values or characteristics may be different each newly created instance of it. In the example above, our diverse information is contained in our public variables or fields that hold what we pass to them in memory. Our variables and method string declaration is known as a data type. In VB the As keyword specifies the data type, in C# the data type precedes the variable name and or method.

The term for using a class, as many times as you wish, each time with different characteristics, is called instantiation or creating an object for use. So the first time the house class is instantiated and assigned it can hold the properties for example, HseSize of "Big" and an HseColor of "Red", and when re-instanced could hold some other very different information. So out of our one House class, we've build two houses all with different characteristics; one class - many uses.

Variables as we've seen have the ability to hold one set value. Class properties unlike their close relatives variables, as we'll examined a little further down, extend the functionality and pretense by allowing you to carry out conditional checking within them preventing any faulty values passed in. Therefore, you can do much, in turn furthering the nature and objective of OOP, as we'll soon examine in the next section.

All we've discussed still needs to be demonstrated real world. Therefore, we'll now build two houses, each with its own particular qualities. So here goes.

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 desktop applications. Till now Jimmy has authored nearly two dozen .NET articles, published on Dot Net Junkies, 4 Guys From Rolla, Sitepoint, MSDN Academic Alliance, Developers.NET, The Official Microsoft ASP.NET Site, and here on Developer Fusion, covering various unique and advanced techniques on .NET.

Comments

  • Re: [4341] The Quick & Dirty .NET Guide to C#/VB OOP

    Posted by Yoenuts on 25 Jul 2006

    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 ...

  • Re: [4341] The Quick & Dirty .NET Guide to C#/VB OOP

    Posted by ggorcsos on 22 Jun 2006

    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 ther...

  • Posted by James Crowley on 20 May 2005

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

  • Posted by DMarko1 on 22 Apr 2005

    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. - [url="http://www.developerfusion.co.uk/show/4676/1/"]Building a...

  • Confusion about this article (Get ,Set)

    Posted by aalhussein on 22 Apr 2005

    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 )
    W...