Hello.

Could someone help me on how to create classes in VB6?
I need to create a class that would enable me to add 2 numbers then show the sum... simple enough? but im having a hard time looking for tutes out there. I could easily learn with some examples...

A walkthrough would be greatly appreciated. Links will also be a great help.

Thanks in advance!

Recommended Answers

All 7 Replies

See the Attached file.

Study the code and read the comments and it will walk you through creating a class

See the Attached file.

Study the code and read the comments and it will walk you through creating a class

Thanks for the code!

Here are some more clarifications(if that's what it should be called) or inquiry:

Do i always have to put my classes in a class module? Or can i have declare classes in a module? my prof. told me u can declare a class (vb6) in a module... i had show him a program i made which was like the one you gave me but he told me to make a class in a module...

Heres my program:
frmMain.frm
has 3 textboxes(txtrate, txtHours, txtPay) and a command button (cmdCompute)

Public gf_MyPay As New CCompPay

Private Sub cmdCompute_Click()
If txtHours.Text <> "" And txtRate.Text <> "" Then
    If CInt(txtHours.Text) > 0 And CDbl(txtRate.Text) > 0 Then
        gf_MyPay.NoHours = CInt(txtHours.Text)
        gf_MyPay.Rate = CDbl(txtRate.Text)
        txtPay.Text = gf_MyPay.Pay
    Else
        MsgBox "Error: Zero Value Found...", vbOKOnly, "May Error po"
    End If
Else
    MsgBox "Error: Blank information Found!!!", vbOKOnly, "May Error po"
End If
End Sub

and my class module:
CCompPay.cls

Private m_NoHours As Integer
Private m_Rate As Double
Private m_Pay As Double

Public Property Get NoHours() As Integer
NoHours = m_NoHours
End Property

Public Property Let NoHours(ByVal iNewValue As Integer)
m_NoHours = iNewValue
End Property

Public Property Get Rate() As Double
Rate = m_Rate
End Property

Public Property Let Rate(ByVal dNewValue As Double)
m_Rate = dNewValue
ShowPay
End Property

Private Sub ShowPay()
m_Pay = m_NoHours * m_Rate
End Sub

Public Property Get Pay() As Double
Pay = m_Pay
End Property

Public Property Let Pay(ByVal dNewValue As Double)
m_Pay = dNewValue
End Property

Is this correct? i does work but my professor told me it was not a class... im a bit confused about it. its the only way i know how to create a class in VB6.

any enlightment about this?

Thanks again

Ahh, Ok...

OK what he is saying to do is add module.bas


Decalere your code a s a function...

So when you goto command_button1 you can say

fucntion1()

and it will run the code.

Reason being is that now you can call function1() throughtout your entire project.


So you take that code turn it into a function and stick it in a module.

Well in this case I am saying function but yo ucan make a class this way as
well...

The genral idea your prof is saying is to be able to ref class or fucntion throughout the entire project through a single module

See the Attached file.

Study the code and read the comments and it will walk you through creating a class

hi,

this helped me a lot to understand class structure for vb6.

Thanks.
Mahavir Shah

Pls can you tell me what is class module how does it works and any references also about connecting oracle SQL plus and VB6 as i hve connected VB with SQl but when i insert any data in SQL in VB the data doesnt gets updated (i have used ADODC for connecting)
Pls help me out im having a project on this...

sbs_1993, please do not "hijack" someone elses post. Start your own post in the forum, and we will try and help where we can. To answer your question on the steps needed, herewith -

Steps:
(1) Open a New Project in VB6 and click on the icon "ActiveX DLL"
This will open up and automatically put a class module in your
project.
(2) Click on Project and then onto Project1 properties.
(3) Change the Project Name to the name of your dll file.
Example (MyDll) Then click on ok.
(4) In the Properties window for Class1, change the Instancing
property to 6 - GlobalMultiUse.
(5) Put in your public functions in the coding window.
NOTE: Any function or sub that is public is what you will have
access to. If it is private, you cannot access these functions.
Only the code inside the dll can.
(6) After putting your public functions in, compile the dll.
NOTE: Make sure you have a copy of your new dll in the
"c:\windows\system" directory.
(7) Open up MS-DOS and change the directory to "c:\windows\system"
NOTE: This will register your new dll.
(8) Type in: regsvr32 c:\windows\system\MyDll.dll
NOTE: Where it says MyDll.dll, you would put in your dll filename.

**************************************************
That's it! you have created your dll.
Now it's time to access your functions.
**************************************************

(1) Close out of everything and start a new project in VB6.
Click on the icon "Standard EXE"
(2) Click on Project and then onto References.
(3) Find your dll name in the list, check it and click on ok.
(4) You will now have access to all your public functions and subs.
EXAMPLE: If you had a Public Function like below in your dll:
Public Function Hello()
MsgBox "Hello"
End Function

You could use this call from your standard exe below:

Private Sub Command1_Click()
Hello
End Sub

**************************************************
Dependancy Information. (Optional)
**************************************************

NOTE: Because your dll is new, VB6 does not have any dependancy information
on it. This information is what's used to create a setup for your
program.

(1) Rather than clicking on Standard Setup Package click on
Dependancy File in the Package and development wizard.
(2) Go through the steps and when it comes up saying that it could
not find any information on your dll, click on ok.
(DO NOT CHECK THE BOX NEXT TO IT!)
(3) After all the steps, it has created a .DEP file in the path
you told it to.
(4) Open this file in Notepad and copy the bottom part of the data
to the clipboard.
NOTE: The data will look like this:

[MyDll.dll <0009>]
Dest=$(WinSysPath)
Date=11/15/00
Time=10:21
Version=1.0.0.0

(5) You will need to copy this information to your VB6dep.ini file
NOTE: If you copied it to your clipboard, you can now paste it out
in the VB6dep.ini file.
NOTE: This file is usually located in:
C:\Program Files\Microsoft Visual Studio\VB98\Wizards\PDWizard\VB6dep.ini

(6) After copying that information into VB6dep.ini, save it.

***************************************************
That's It! You can now make all the setups you want
and VB6 will automatically register your dll file
in the users computer that you installed your program
to.
***************************************************

If you need more info, google it. There is plenty help and notes out there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.