Library tutorials & articles

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

A House Building Class

Now that you've already created your house.cs source file, next run the command below to invoke .NET compiler to compile your source file into a DLL we can use, and is located in our root bin folder.

[C#]
csc /t:library /out:c:\inetpub\wwwroot\bin\house.dll house.cs /optimize

[VB]
vbc /t:library /out:c:\inetpub\wwwroot\bin\house.dll house.vb /optimize

Additionally, all code examples throughout are to be compiled in this manner, dependent of course of the language of your choice.

Now that we have the "materials" for our house let's build two houses out of one blueprint if I may. Place the preferred language code below in a .aspx page named House.aspx , then run it in your browser.

[C#]
<%@ Page Language="C#"%>
<html>
<head>
<title>Building a House</title>
</head>
<script language="C#" runat="server">
protected void Page_Load(Object Source, EventArgs E) {
  // Build Jim's house
  House JimsHouse = new House();
  JimsHouse.HseBuilder = "Jim";
  JimsHouse.HseSize = "huge";
  JimsHouse.HseColor = "blue.";
  Response.Write (JimsHouse.Summary() + "<BR><BR>");
  // Clear the object
  JimsHouse = null;
  // Build Lumi's house
  House LumisHouse = new House();
  LumisHouse.HseBuilder = "Lumi";
  LumisHouse.HseSize = "simply breathtaking";
  LumisHouse.HseColor = "however she wants.";
  Response.Write (LumisHouse.Summary());
  // Clear the object
  LumisHouse = null;
}
</script>
</body>
</html>

[VB]
<%@ Page Language="VB"%>
<script language="VB" runat="server">
Protected Sub Page_Load(Source As Object, E As EventArgs)
  ' Build Jim's house
  Dim JimsHouse As New House()
  With JimsHouse 'VB syntax shortcut
   .HseBuilder = "Jim"
    .HseSize = "huge"
    .HseColor = "blue."
  End With
  Response.Write (JimsHouse.Summary())
  ' Clear the object
  JimsHouse = Nothing
End Sub
</script>

And our results should look like this:

To embark on OOP reusability, by using the new keyword we declared and instantiated our object (bring our class to use) - this is know as early binding (the prefered method of dealing with objects).

Thus, we write in C# House JimsHouse = new House() or Dim JimsHouse As New House() in VB; observe C#'s case preference. Essentially, what we're saying here is create a "new House()" object called JimsHouse, that calls our classes default constructor - new House() (which is the same as House.new() ), with no parameters, whereas custom constructors utilize parameters.

Then we define or pass its characteristics to the fields in our class. Once we do all this we next call our summary method to return to us our stats. After this, as in every example throughout this article, you must always clear your objects. C# uses the object = null; statement, whereas in VB it's object = Nothing to do this.

So that's it more or less, the basis of object creation and OOP. Not too shabby... but wait.

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

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

  • Nov 18

    15 Minutes of Fame

    Dresher, United States

    This is a yearly tradition. We select 10 of the favorite speakers from monthly meetings, code camps, and hands on labs. Each one does a 15 minute talk on their favorite .NET technology. This is our 10th anniversary so we plan a gala event with special prizes and refreshments.

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