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 PageLoad(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 PageLoad(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