Ads 468x60px

.

Featured Posts

The Basic Basics

In the whirlwind tour in Chapter 2 I touched on the concept of object-oriented programming. It rears its head even here, but in a good way. You see, every type of data you work with has a class defined for it in the .NET class library, and as you saw in the previous chapter, classes come with handy methods that let you do things.


Every class in the .NET Framework is based on, directly or indirectly, one specific class called System.Object. It all comes back to the topic of inheritance, which I touched on briefly in the preceding chapter. You are a unique individual, as am I, but we are both human. You could say then that we inherit all the traits every other human being has, and then add to those our own unique properties and abilities. I could also say that I indirectly inherit the traits of any other human because there’s a lot between me and the basic

definition of a human. Given that I’m a dark-haired English guy with a penchant for technology, you could say that my inheritance chain looks like this:

Human ➤ Man ➤ EnglishMan ➤ DarkHaired ➤ Nerd ➤ Pete

I am thus indirectly based on a human. Every other object in that chain is also based on a human, but at each stage of the chain more and more unique attributes are added, finally ending up with little old me.

In the .NET Framework every class has at its very core the System.Object class. What this means is that everything System.Object can do, every single class in the .NET Framework can also do. It’s kind of like saying that because humans can walk on two legs, see with two eyes, hear with two ears, and hug with two arms, so can pretty much every other human subclass (like me).

Why is this important, I hear you scream. Well, System.Object brings with it a couple of handy functions, such as ToString(). Take a look at this.


Try It Out: System.Object Stuff

Start up a new console application and then key in some code to set up variables to hold a bunch of different types of data. Because I haven’t covered just how to do that in any real amount of detail just yet, go ahead and copy the following code so that your Main() subroutine looks like mine:

Don’t worry too much about what some of the code means at this point. Next, define a new subroutine called ShowMe() that looks like this (add it beneath the Main() subroutine):

Can you see where this is going yet? ShowMe() takes a single object as a parameter. Because every type of data you work with in VB Express derives from Object, you can pass literally any kind of data to this. In addition, every type in VB Express has a ToString() method (because the base System.Object class has one), so ShowMe() can get any object to convert itself to a string (lump of text) and write it out to the console.

It gets even better than that. In the call to Console.WriteLine() in the preceding code, you didn’t actually have to put ToString() on the end of the object you passed in. System.Object has a ToString()

method, which in turn means every class you’ll ever use or create has one too. This in turn allows the .NET Framework developers at Microsoft to make handy assumptions. If you don’t pass a string to the WriteLine() method, the code inside that method will call ToString() on whatever you pass to it. For example Console.WriteLine(myObject) produces the same result at runtime as Console.WriteLine(myObject.ToString()). I like to put ToString() on code like this because it makes it clear to me and anyone else reading the code that I am actually after the string version of my object.

Go ahead and add the missing lines of code to the Main() subroutine to call the ShowMe() function:

Run the program now and you’ll see the console window in Figure 3-1.

This is just shown to demonstrate the fundamental concept that everything is an object. I wouldn’t recommend you go around writing a bunch of methods like ShowMe(), though, that take Objects as parameters. There’s quite a performance hit when you do this, and it all has to do with something called boxing and unboxing.For now, let’s look at the most common specific data types that you’ll be working with.