Class question

ARC

Well-known member
Joined
Sep 9, 2006
Messages
63
Location
Minnesota
Programming Experience
Beginner
I was wondering if someone could try explaining the concept of creating your own class to me in laymans terms. I'm having a hard time grasping the concept. I have examples of it in code, and I can see the logic somewhat but, i dont really understand how it works exactly.

From my understanding (and I just go with the flow), you create a class, and then give it properties? and then you can call that class, and modify it's properties.

If x = carModel.Ford then

as apposed to

if xtype = car then
if xModel = ford then
...
...
 
OOP is extremely easy because it is based on the real world. Types and objects in OOP behave just like types and objects in the real world. As an exercise, describe to yourself what a car is. Describe what a car is and what a car does.

Congratulations! You just described the Car class. If you were to write down what you just described then what a car is would be the properties and what it does would be the methods.

Now look out on the street. See all the cars driving by? Each of those is an object; an instance of the Car class. Each of those objects has all the properties of the Car class, although each object may have a different value for each of those properties. For instance, each Car object has a Doors collection. Some cars would have four doors in that collection while others would have two.

So, you have the car class, which is basically a description of what Car objects are and do:
VB.NET:
Public Class Car

    Public Property Doors() As DoorCollection

    Public Function Start() As Boolean

End Class
Now you can declare variables of type Car:
VB.NET:
Dim myCar As Car
and create Car objects:
VB.NET:
myCar = New Car
 
ok, so the class section of the code holds all of the properties of said class... As well as functions that only it can perform... Are the functions called automatically when the class object is refrenced or do you have to call it seperately like

myCar.Start()

And now im looking into what the Get and Set is all about, as visual studio was kind enough to write the whole property section for me after I let it know i wanted to put a property in my class.
 
Last edited:
Think about it. Does a car start automatically when it's built? No, you have to explicitly start it. Objects are the same. If you want them to do something then you have to tell them to do it by calling the method. What if a class had a Start method and a Stop method? Would expect them both to be called "automatically"? When you instantiate a class, i.e. create an object of that class, it will be in its default state. What that default state is is up to the author of the class. If you're the author you can initialise member variables either where they are create or in a constructor.

As for properties, they are a combination of variables and methods. From the outside they appear like a variable, in that you can just get and set their value. From the inside they appear like a method, allowing the author to perform any task they like which may include, but is not limited to, getting or setting a variable. One of the most common tasks that gets perform within a property is raising an event. Take the Text property of a TextBox. If it was just a variable then when it was set that would be it, but because it's a property when it's set the value can be transferred to a variable for storage, then the TextChanged event can be raised to notify the user that the value of the property has changed.

In a language like Java, which doesn't support properties, the author must create individual methods to get and set a variable value in order to perform other tasks. The names of these methods usually start with "get_" and "set_". The Get and Set methods within a property are the equivalent, but they are masked from the outside within the one easy-to-use property.
 
hmm...

So first you have the class

VB.NET:
Public Class Car

End Class

and then you create a car object by doing

VB.NET:
Dim myCar As New Car

And then to add a property to the Car Class (which also means that it is a property of myCar?) you say this...

VB.NET:
Public Class Car

Public Property VIN() As Integer = 123456789

End Class

But then how do you access that value of the VIN property? And I guess I still dont understand the Get and Set purposes of the code.
 
What you have there is not a property. A Car class with a Vin property would look like this:
VB.NET:
Public Class Car

    Private _vin As Integer

    Public Property Vin() As Integer
        Get
            Return Me._vin
        End Get
        Set(ByVal value As Integer)
            Me._vin = value
        End Set
    End Property

End Class
So when you do this:
VB.NET:
Dim myVin As Integer = myCar.Vin
you are getting the Vin property of the Car object referred to by the myCar variable. Getting a property means calling its Get method, which in this case returns the value of the private _vin variable.

When you do this:
VB.NET:
myCar.Vin = myVin
you are setting the property, which means calling its internal Set method. In this case that method sets the private _vin variable.

It is very common practice to use a public property to expose the value of a private variable like that and the reasons are several. One that I mentioned earlier is to enable you to raise an event when the property value changes, e.g.
VB.NET:
Public Class Car

    Private _vin As Integer

    Public Property Vin() As Integer
        Get
            Return Me._vin
        End Get
        Set(ByVal value As Integer)
            If Me._vin <> value Then
                Me._vin = value
                Me.OnVinChanged(EventArgs.Empty)
            End If
        End Set
    End Property

    Public Event VinChanged As EventHandler

    Protected Overridable Sub OnVinChanged(ByVal e As EventArgs)
        RaiseEvent VinChanged(Me, e)
    End Sub

End Class
Now, whenever you change the value of the Vin property the Car object will raise its VinChanged event to notify any code that's listening that the value has changed. This allows such things as bound controls to update the value they're displaying to the user.
 
Could you then hypothetically create a new car object every time you puch a button, and stuff it into a (let's say for example)
VB.NET:
Public carArray() As Object
Public totalCarCount = 0

Dim aCar As New Car
        Dim loopCounter As Integer = 0

        aCar.VIN = 123456789
        
        totalCarCount += 1

        carArray(totalCarCount) = aCar

        For loopCounter = 0 To totalCarCount Step 1
            Me.RichTextBox1.AppendText((carArray(loopCounter).ToString) & Environment.NewLine)
        Next counter

I see that i'm saying .ToString to an object (because that's what's in the array)... but maybe there's another way to do it.
 
Last edited:
Back
Top