Results 1 to 13 of 13

Thread: Who's afraid of classes?

  1. #1
    Guest

    Talking 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.

  2. #2
    Guest
    Anyone?

  3. #3
    Hyperactive Member
    Join Date
    Feb 2000
    Posts
    284
    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.

  4. #4
    Guest
    How can you make it so every property of the class is a field?

  5. #5
    Lively Member
    Join Date
    Jun 2000
    Location
    A caravan park in the Midlands (UK)
    Posts
    101

    Smile

    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.

  6. #6
    Guest
    Good, I knew my herd will come in handy

  7. #7
    Addicted Member
    Join Date
    Jan 2000
    Location
    Oshkosh, WI
    Posts
    163

    Talking

    Classes allow you to share code easily among multiple projects (assuming their compiled into COM objects).

    If you encapsulate your business logic into the classes it then easy to update the applications that you the objects without recompiling them.

    Just my 2 cents

    Glenn D
    Development/Analyst

  8. #8
    Fanatic Member
    Join Date
    Sep 2000
    Location
    UK.
    Posts
    728

    Smile 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]

  9. #9
    Guest
    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!

  10. #10
    Guest
    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?

  11. #11
    Guest
    Hold on, so from what I see, a class is sort of like a a UserControl just without the GUI. Right?

  12. #12
    Guest
    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.

  13. #13
    Guest
    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
  •  



Click Here to Expand Forum to Full Width