|
-
Sep 8th, 2000, 07:17 AM
#1
Who's afraid of classes?
Ok, this may seem to a lot of you like a stupid question, and I don't blame you.
I've been programming in VB for about 4 years now (about 1 year was for fun only) and I've never used classes in any of my projects (except DLLs). I used everything but classes. I don't have anything against them, and I'm not afraid of them , I just don't understand what's so special about them. When and why do I use them?
Can anyone answer this question please?
I see everybody use classes and they seem very useful, I just don't know how/when to use them.
BTW: Please don't flame me about this.
-
Sep 8th, 2000, 08:30 AM
#2
Anyone?
-
Sep 8th, 2000, 08:56 AM
#3
Hyperactive Member
The most obvious reason I can think of for using Classes is when you have a database type project. I would set up a class for each table. Each class represents a table object, each property of the class represents a field.
Ok it's very basic but it's as good a start as any.
-
Sep 8th, 2000, 09:02 AM
#4
How can you make it so every property of the class is a field?
-
Sep 8th, 2000, 09:32 AM
#5
Lively Member
If you've a table with:
Name
Age
Tel No.
Email Addresss
you'd have property let/gets for each of those within a class. The class maybe something like 'clsPerson' and you may then have a class which hold a collection of these classes called 'clsPersons'.
Having a class represent a table on a DB is all very well but it does require you to have a good, normalised, structure to your DB.
In the main classes are pretty handy but I've come across some which use, gulp!, Global variables. If you come across a class using a global you are entitled to shoot the author.......but only in Uzbekistan where they've just passed a law allowing this. Oh in Wales you are permitted to batter the author into unconsciousness with a small sheep.
Anakim
It's a small world but I wouldn't like to paint it.
-
Sep 8th, 2000, 09:40 AM
#6
Good, I knew my herd will come in handy
-
Sep 8th, 2000, 04:23 PM
#7
Addicted Member
-
Sep 8th, 2000, 04:51 PM
#8
Fanatic Member
OOP
Classes let you incorporate 'basic' OOP (Object Orientated Programming) technicues into your program in VB. In my opinion, they let you structure your programs more efficiently than other ways that exist.
Bye
Digital-X-Treme
Contact me on MSN Messenger: [email protected]
[VBCODE]Debug.Print Round(((1097) - ((55 ^ 5 + 311 ^ 3 - 11 ^ 3) _
/ (68 ^ 5))) ^ (1 / 7), 13)[/VBCODE]
-
Sep 8th, 2000, 05:24 PM
#9
Classes are great when you get into complex things such as games. For example, in a game, you would need to create a player. A player can Jump, Attack, Defend, Run etc. There are also things that describe the character such as Hitpoint, Strength, Defence, Speed etc.
All of this can be organized into a class, for example:
Code:
'local variable(s) to hold property value(s)
Private mvarHitpoints As Byte
Private mvarStrength As Integer
Private mvarDefence As Integer
Private mvarSpeed As Integer
Public Sub Attack()
'Attack someone
End Sub
Public Sub Jump(Height As Byte)
'Jump
End Sub
Public Sub Run(Speed As Integer)
'Run
End Sub
Public Property Let Speed(ByVal vData As Integer)
mvarSpeed = vData
End Property
Public Property Get Speed() As Integer
Speed = mvarSpeed
End Property
Public Property Let Defence(ByVal vData As Integer)
mvarDefence = vData
End Property
Public Property Get Defence() As Integer
Defence = mvarDefence
End Property
Public Property Let Strength(ByVal vData As Integer)
mvarStrength = vData
End Property
Public Property Get Strength() As Integer
Strength = mvarStrength
End Property
Public Property Let Hitpoints(ByVal vData As Byte)
mvarHitpoints = vData
End Property
Public Property Get Hitpoints() As Byte
Hitpoints = mvarHitpoints
End Property
Now we can use that class to create a player
Code:
'Create a player
Dim Player1 As CPlayer
Set Player1 = New CPlayer
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyUp Then
'Jump at (at a height of 15 units) when the UP key is pressed
Player1.Jump (15)
End If
End Sub
Private Sub Form_Load()
'Set the properties of the character
With Player1
.Hitpoints = 25
.Strength = 20
.Speed = 10
.Defence = 10
End With
End Sub
Remember, since we have a 'template' of a Player, we can create as many as we want.
Code:
Dim Player1 As CPlayer
Dim Player2 As CPlayer
Dim Player3 As CPlayer
Dim Player4 As CPlayer
'...
Dim Player100 As CPlayer
It's much easier (and faster) to create a class for a player than to create 700 different variabes/functions for each player!
-
Sep 9th, 2000, 07:14 AM
#10
Thanks guys 
And let's say I have a DB project which is not very complex (One connection, and about four recordsets that don't have anything to do with each other), will a class be useful here?
-
Sep 9th, 2000, 08:04 AM
#11
Hold on, so from what I see, a class is sort of like a a UserControl just without the GUI. Right?
-
Sep 9th, 2000, 08:09 AM
#12
You have the right idea, but remember; a UserControl is a type of class too, so following sentence would make more sense.
A UserControl is a class with a GUI.
-
Sep 9th, 2000, 08:21 AM
#13
Thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|