|
-
Aug 17th, 2000, 08:06 AM
#1
Thread Starter
Fanatic Member
Hi,
I am trying to find some example(s) that will help me understand class modules. I've read through a few tutorials, but my brain is rejecting the concept.
I use code modules all the time for shared subroutines, but do not understand when I would need to use a class module.
Does anyone have a site or book or real world example they could recommend that explains this to a beginning VB programmer ?
Thanks for any leads.
-
Aug 17th, 2000, 08:16 AM
#2
-
Aug 17th, 2000, 08:58 AM
#3
Frenzied Member
CLass modules are a bit of a mindflip from standard coding, the principal is that you want to keep some code tgether with your data.
When you program a class module it's a little bit like making your own type of variable, exept rather than simply storing data your Class can calculate aspects of the data, format it or do anything you want with it.
Here's a real world example, I'm doing a project for an Educational Softwere company at the moment, and part of what I'm doing is about Trigonometry, Triangles and other fun things.
But there is a bit of a problem with angles, The users of the program want to see their angles in Degrees and Minutes but VB does all it's Trig calculations in Radians,.
If I used a standard module for this I would need all sorts of functions to cope with this properly and I would need to call them all the time, this would make the coding of the project very messy and difficult to follow, not to mention dificult to cade.
So I made a class module to act as an Angle data type in my project.
I wanted something like this.
- I wanted the angle to be stored in degrees and minutes (a minute is a 60th of a degree BTW) I don't need any more accuracy than this and I don't want any worries about hidden half minutes adding up to make whole minutes.
- I want a Degrees property and a minutes property for the value of the angle for me to work with, If I add or subtract to the minutes property and it exeeds 60 or falls below 0 I want the degrees to be updated for me.
- If the value of the angle exxeds 360 or falls below 0 I want it to change to the correct angle.
- I want a property which gives me a string representation of the angle So I can print it easily to the user.
- I want the default property of the class to be its angle in Radians in this way if I have 3 instances of my class A,B and C I can perform arithmetical oprations easily (C = A+B, Msgbox Cos(A) etc)
This is what I came up with
Code:
Option Explicit
Dim m_Degrees As Integer
Dim m_Minutes As Integer
Public Property Let Degrees(New_degrees As Integer)
'we have to do the mod function twice to catch -ve numbers
m_Degrees = ((New_degrees Mod 360) + 360) Mod 360
End Property
Public Property Get Degrees() As Integer
Degrees = m_Degrees
End Property
Public Property Let Minutes(New_Minutes As Integer)
m_Minutes = New_Minutes Mod 60
Me.Degrees = Me.Degrees + Fix(New_Minutes / 60)
End Property
Public Property Get Minutes() As Integer
Minutes = m_Minutes
End Property
'This is the Value of the Angle in Radians
'It is set as the Default Property.
'This allows us to add angles and perform trig operations on it directly
'It is hidden to keep syntax consistent
Public Property Let Radians(New_Radians As Double)
Me.Degrees = 0
Me.Minutes = (New_Radians * 10800 / PI)
End Property
Public Property Get Radians() As Double
Dim dblTemp As Double
dblTemp = Me.Degrees + (CDbl(Me.Minutes) / 60)
Radians = dblTemp * PI / 180
End Property
Public Property Get StringValue() As String
If Me.Minutes Then
StringValue = Me.Degrees & "°" & Me.Minutes & "'"
Else
StringValue = Me.Degrees & "°"
End If
End Property
This works great for me, Copy it and have a play with it, before you use it go to Procedure atributes on the Tools menu, select Rdians from the combobox on the window that comes up, hit the advanced buton and check the user interface default checkbox. Oh and you Need to declare Pi as a public constant in a standard module (it's 3.14159265358979 BTW)
Have a play with that, call the modle clsAngle and Declare one with
Code:
Dim MyAngle As New clsAngle
And it'll solve all your angle problems.
That's the point of class modules, you keep the functions inside your data types so you don't have to call them in Code, It makes the code easier to write and easier to read.
-
Aug 17th, 2000, 09:03 AM
#4
Thread Starter
Fanatic Member
Thanks Megatron and Sam. I'll review the thread and experiment with the code and hopefully it will start to sink in.
I have a feeling once I get it figured out, there will be nothing to it. Just need to get the light to click on in my head.
-
Aug 17th, 2000, 11:47 AM
#5
Fanatic Member
Two months ago, I was like you (didn't know how to use class modules) and now I've got a firm grasp of them (and many other classy objects )
They make your code more readable (I think) and also help if you want ... Ah **** it. You'll only realise how useful class modules are when you come across a reasonably big project and you try to use them.
I'm e-mailing you a sample project with three class modules, a BAS module, and a form.
The main asset of this project is that although there is 1000+ lines of code in the class modules, you only need to make them once and in the form module, there's only about a hundred lines.
-
Aug 17th, 2000, 11:54 AM
#6
Fanatic Member
Damn. You don't have an e-mail do you?
Think of a class module as a PictureBox. It can raise events, it stores its own properties and has its own functions which can act on those properties. It has all the necessary API declares, so your form doesn't have to. And usually all you have to do is add 2 lines to 'start it up' and then you can modify its properties to customise the class. (A class module doesn't have to be something that's hidded, I use many of my class modules to create a visual effect that's customisable.)
If you do get an e-mail address, I'll be happy to send the project to you (it'll be useful to you anyway as it helps create really professional menus)
-
Aug 17th, 2000, 12:07 PM
#7
Thread Starter
Fanatic Member
V(ery) Basic. I would appreciate seeing your sample code. I really want to understand class modules so I will know when they will come in useful.
My e-mail is [email protected].
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
|