Thursday, February 25, 2010

Classes Overview

Today we had to use classes. I will try to explain briefly what classes are, without going into too many details, because this is a deep subject.

First off classes are concepts of "things". A thing can be pretty much anything you can interact with. Chairs, tables, cars and clothes are all classes. Objects are physical representation of those concepts ( or logical, in the case of computers ). The particular chair you are standing on is the physical representation of the "chair" concept. Chairs are things which have several legs and a plate on which you can sit. But the one you are sitting on right now may have a number of legs and style and paint, it is unique ( even more unique when you think about the atoms making your chair ). Class = Chairs and Object = [The one chair you're sitting on]. 

Creating a class is made that way:

Public Class Car  'The class definition

Dim typeOfCar as string 'This is called a Member, it is specifically a property of a class (i.e a variable )


Sub nameCar(byval newValue as string) 'This is another member, it is a procedure.
typfOfCar = newValue


End Sub

End Class


By inserting the following snipper we have now created a Class called Car, it is a very general and broad class right now. It has a single property called typeOfCar that defines what type of car we're talking about. Right now it has no value so it can be anything. It a concept. When we create it, we define the car. We give it a brand, a name, a maximum speed, number of doors and etc. This is called an instance of a car. And creating an instance of a class is as easy as creating any other variable.

Dim MyViper as New Car
MyViper.nameCar("My Big Car")

We now have a living representation of a "car", its a dodge viper and its name is "My Big Car". We have given it a name using our member by accesing it using the syntax Class_Name.Procedure_Name


As you see, an instance of a class is an object. And the class itself that defines the procedures and properties is a Class. By using Object Oriented Programming you can do many things functional programming cannot do. The code you make is a lot neater and well structured. OOP is a lot more complex than that but thats all I'm going to risk explaining else if make some error and give you bad knowledge. That is all for today, hope to see you next time !

No comments:

Post a Comment