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.
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
Public Property Let Windows(ByVal vNewValue As Integer)
intWindowCount = vNewValue
End Property
Public Function Speed() As Integer
Speed = intSpeed
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:
Private Sub Form_Load()
Dim mini As Car
Set mini = New Car
mini.Speed = 50
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
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
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?
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
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
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.
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...
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
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:
Function SumNums (FirstNum as Double, SecondNum as Double) as Double
Dim MyNumA as Double
Dim MyNumB as Double
'Get numbers passed in.
MyNumA = FirstNum
MyNumB = SecondNum
'Sum the numbers and return the result.
SumNums = MyNumA + MyNumB
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:
' As your point of view shifts, so does your vocabulary. When making
' use of the class, it's called an "object" -- a bunch of code wrapped
' together, ready to use.
'Declare object variable and
'load the object into memory (if it's not already).
Dim MyNums as cCalcNumbers 'Declare
Set MyNums = New cCalcNumbers 'Create
'Declare a variable to hold the sum.
Dim MySum as Double
'What's called a function inside the class is called a "method"
'here in the "client."
'Call the SumNums method, adding together two numbers.
MySum = MyNums.SumNums(498,1838)
'Test.
Msgbox MySum
'Reclaim memory.
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...