Results 1 to 11 of 11

Thread: What is a class?

Hybrid View

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780

    What is a class?

    i need to make a class for a project and i was wondering if some one could tell me about classes. i have read about them in some books but they don't give me the clearest picture.

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Classes are modules or containers of code which allow you to define an object.

    ie. when you write "command1.backcolor" - you're accessing a property of the command1 object. At some point, Microsoft has written a class to define a button - what it looks like, what events (such as click, mouseover etc.), what properties (like backcolor, style etc.) it should have to allow you to create a commandbutton object.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780
    ok thanx. is there a place where i can see a simple class that you know of to get an idea of how to program one?

  4. #4
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    VB Code:
    1. Private intWindowCount As Integer
    2. Private intSpeed       As Integer
    3.  
    4. Public Property Get Windows() As Integer
    5.     Windows = intWindowCount
    6. End Property
    7.  
    8. Public Property Let Windows(ByVal vNewValue As Integer)
    9.     intWindowCount = vNewValue
    10. End Property
    11.  
    12. Public Function Speed() As Integer
    13.     Speed = intSpeed
    14. End Function

    That's a very quick example in which I'm exposing a property (like you see in the vb properties window when you add a control to a vb form), as well as a method I can call on to return a value.

    From me writing that, I now have access to play with these 2 from a vb form etc. by using the following code:
    VB Code:
    1. Private Sub Form_Load()
    2.     Dim mini As Car
    3.     Set mini = New Car
    4.    
    5.     mini.Speed = 50
    6.     mini.Windows = 4

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  5. #5
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    This is the best tutorial by far on learning class and and activex designing:
    http://www.developer.com/net/vb/article.php/1539541

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780
    ok i understand all of it but this part
    VB Code:
    1. Private Sub Form_Load()
    2.     Dim mini As Car
    3.     Set mini = New Car
    what is car? i understand that the windows and the speed was set therefore the .speed and the . windows. but the car part is that just a variable that u made from your class?

  7. #7
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  8. #8
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    ahhh suppose I should've uploaded the project! CAR was what I named the class! Sorry for the confusion!!!!

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780
    so what did it do if you don't mind me asking. i think that if i were to understand what it did then maybe i could get a better picture of what is going on.

  10. #10
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Ok the idea behind a class is re-usability, the same idea as is behind module code - you place a public routine in a module & you only need to put:

    call modulename.routine

    out (1 line of code) to run the routine from anywhere in your app. Classes are, in proncipal the same, but the main differences between the 2 are:

    1) Classes can be instanciated - in English, they are objects themselves which you can create and destroy using the set keyword.
    2) They can expose events whereas bas modules cannot
    3) They can expose properties whereas bas modules cannot

    I've altered the car sample a bit so there's an actual use for it now - rather than a sample code to show how a class looks, this one shows how you can create serveral instances of the same class and use them in an app, hopefully giving more of an insight into how they can be used...
    Attached Files Attached Files

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  11. #11
    Junior Member
    Join Date
    Jan 2004
    Posts
    25
    Dubya007,

    A class holds a bunch of code. Another developer -- if not yourself -- can later make use of this code in their project. Code within a class must be organized in a certain way and should, ideally, stand on its own so that it can be used over and over from one project to another.

    An overly simplistic example: Say you wanted to write a class to add together any two numbers.

    To do so in VB, add a new blank class module. And in this class module write a function to add the numbers.

    VB Code:
    1. Function SumNums (FirstNum as Double, SecondNum as Double) as Double
    2.  
    3.   Dim MyNumA as Double
    4.   Dim MyNumB as Double
    5.  
    6. 'Get numbers passed in.
    7.   MyNumA = FirstNum
    8.   MyNumB = SecondNum
    9.  
    10. 'Sum the numbers and return the result.
    11.   SumNums = MyNumA + MyNumB
    12.  
    13. End Function

    Save the class and give it a name (within the project) of, say, cCalcNumbers. In your project, create a new form and drop a command button on top. In the button's click event, put this code to test the class.

    VB Code:
    1. ' As your point of view shifts, so does your vocabulary. When making
    2. ' use of the class, it's called an "object" -- a bunch of code wrapped
    3. ' together, ready to use.
    4.  
    5. 'Declare object variable and
    6. 'load the object into memory (if it's not already).
    7.   Dim MyNums as cCalcNumbers 'Declare
    8.   Set MyNums = New cCalcNumbers 'Create
    9.  
    10. 'Declare a variable to hold the sum.
    11.   Dim MySum as Double
    12.  
    13. 'What's called a function inside the class is called a "method"
    14. 'here in the "client."
    15. 'Call the SumNums method, adding together two numbers.
    16.   MySum = MyNums.SumNums(498,1838)
    17.  
    18. 'Test.
    19.   Msgbox MySum
    20.  
    21. 'Reclaim memory.
    22.   Set MyNums = Nothing

    You could easily extend this class by adding additional functions: a function to subtract one number from another, a function to multiply two numbers together, a function to get the square root of a number, etc. Again, an overly simplistic example -- there's much more to know -- but maybe this can be a place to start, conceptually speaking...
    Last edited by XYZ-R; Mar 25th, 2005 at 11:10 PM.

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