In object oriented programming many people mistake the class constructor as the ‘object’. This thought is a misconception as the class constructor is more like a blueprint to build objects from. You can create multiple objects from a single class constructor and utilize shared properties from the constructor in those different objects. Today we will be creating a class constructor and then we will instantiate new objects from that constructor and assign them unique property values. Then we will see how the two different objects are sharing a property that was defined in the constructor.

Let’s get started!

Creating The Project

Since we’re coding this project in ASP.NET we’re going to be using Visual Studio 2012. If you don’t have access to Visual Studio, you can download a free version of Visual Studio Express for Web from Microsoft’s website.

Our first step is to open up Visual Studio and create a new project. We’re going to select C# as our programming language, and then select Web on the sidebar and ASP.NET Empty Web Application as the application type. Finally, we’ll name the project Cars.


Set Up

After we have a new project, you’ll probably notice that the solution explorer doesn’t have any application files. Let’s begin by creating a Default aspx page and it’s corresponding code-behind. To do this, we must right click on the Cars directory in solution explorer, hover your cursor over Add, and then click on New Item.


Now you will be prompted with a new screen asking you which file type you would like to add to the solution. If it’s not already selected, click on Web in the left sidebar under Visual C#. Then, click on Web Form from the listed items in the center of the window. You’re going to want to name this file Default.aspx, the code-behind will be automatically generated as Default.aspx.cs. Click Add and you will notice that these two files have been added to your Solution Explorer.


The last step in setting up our project will be creating the Car constructor class. To do this, add another item to the solution just like we did previously for Default.aspx.


Now, instead of creating a Web Form, we’re going to create a Class. To do this, click on Code within the left sidebar under the hierarchy of Visual C#. Click on Class at the top of the item list, and name your class Car.


Now that we have our files structured properly, we can move on to the fun part!

Coding the Constructor

For the purpose of this tutorial, we’re just going to be using variables to get and set the properties of each object. This means that all we have to specify in our constructor class, are the variables in which we want to store data. So open up Car.cs and type the following within the public class Car brackets:

As you can see, we’ve specified a common property called Wheels for any Car object that is instantiated. This allows us to preemptively define the wheel count on every car as the integer 4. After that, we define a few nullifiable integer properties by specifying their type with a question mark appended as a suffix. A nullifiable property is a property that is able to return null if it has been called by a getter before it has been set by a setter. We specify that we are allowing these properties to be get and set after the property name has been specified.

Now, your Car.cs file should look something like this:

Creating Our Objects

Now that we have our constructor, let’s instantiate some objects from it inside our code-behind. Navigate to the Default.aspx.cs code-behind and within the Page_Load method, type the following:

As you can see, we have instantiated a new Car object named bmw. The variables specified in the Car constructor can now be set and retrieved by methods of the same name. We set all of the variables specific to this Car instance utilizing these methods.

Let’s add a few more cars to our garage, place the following code below the first object instantiation.

Notice how we didn’t specify the Wheels property of each Car object, as this property is not editable.

Displaying the Information

So now we want to display the information of our cars on our web page. To do this we could print out each property one by one, but since we’re using good DRY practices, let’s use an array of all the cars, and a foreach loop.

First instantiate your array:

Then, create a foreach loop:

Your Default.aspx.cs file should now look something like this:

Notice how we have specified the Wheels property of each element even though we haven’t set that element in each of our Car instances. So let’s dissect what’s happening above. For each of our cars in our theCars array, we are outputting a paragraph containing the car information and placing that paragraph inside an html element with the id of myCars. But wait, we haven’t made that element, so let’s go ahead and do that. In Default.aspx find the empty div within Form1 and give it the following properties:

Now that we have an HTML Controller we can pass our foreach loop’s generated code into the myCars div element.

Running The Program

It’s time now that we can run our program and see what our code generates in the browser. To run your code, navigate to the green play arrow at the top of Visual Studio, and click it. Visual Studio will now create a test server for you to view your application on, and then run any code that is within the Page_Load method in your code-behind.

As you can see, we get the following output in our browser:

Concluding this tutorial, let’s discuss the output above. As we can see our foreach loop did it’s job outputting each object’s properties onto our ASP.NET web page within a paragraph tag. Hopefully you have noticed, that each object’s Wheels property was outputted as 4. Since the property was not editable and belonged to the constructor of each of our objects, it was passed along to each of our objects automatically. Which means every single object created with the Car constructor will have the property Wheels and the value set to the integer 4.

Amazing isn’t it? In this tutorial we have learned that the class constructor and the actual object it creates are two different things, and that the properties assigned to the constructor are automatically passed down to each object that was instantiated with that constructor. These practices are the basis for object oriented programming, as you now have the ability to create objects that share properties using the same class instead of two separate classes.

Thanks for following along and feel free to download the source files below!

Download Source Files