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 House Building Class

DMarko1

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.

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 &amp; 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 &amp; 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...