jump to navigation

Let’s Start Simple January 11, 2007

Posted by gcorbin in C#.
trackback

Before we talk about asp.net and all its cool features, we need to understand a little bit about the programming languages that can be used. The two languages that Microsoft provides are C# and VB.net. Either language will work just fine, but my preference is C#. So, all the sample code that I provide will be written with C#.

Ok, let’s get started. In this posting I wanted to start simple by just discussing the C# language and some of its uses. The best way to do this is with an example. The example below shows deals with creating some objects and defining them. I decided to use racecars as my example objects. Take a look at the code below for further details.

 This sample was created using VS2003, but will work in VS2005. For the purpose of creating a simple sample I choose to make it a console application, but the code will work very much the same if it were called from an asp.net page.

Listing 1

blog2a.JPG

The source code for this can be downloaded here. This sample shows a simple Class called FordRaceCar. This Class has many properties and methods that can be called and set. A FordRaceCar Object is created and set in Main( ) method of the RaceTrack Class.

This Main( ) method is the entry point for the application. Its equivlant to PageLoad( ) in asp.net

The main problem with this code is what if we wanted to create a ChevyRaceCar? We could take all the code for the FordRaceCar Class copy it, and call it ChevyRaceCar Class. The problem here is we would now have a lot of duplicated code. This would lead to poorly maintainable code.

A better solution is to use Inheritance. We would create one base class that has all the methods and properties that would apply to RaceCars in general, and then we could inherit this base class into our specific FordRaceCar and ChevyRaceCar Class. This would avoid the duplicate code. We are also going to have an interface to define our base RaceCar class. The Interface is nothing more than a contract that says what a base RaceCar must have. In this case, the only property being enforced here is “Tires”. This means that a base RaceCar must have code to define the car tires. The interface does not contain any implemented code, only the contract. All the source code below can be downloaded here.

 blog1b.JPG

 blog1c.JPG

Listing 6

 Listing 5

Advertisements

Comments»

No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: