-
Sep 21st, 2007, 10:04 AM
#1
Thread Starter
Addicted Member
[RESOLVED] how can i use classes in VB 6.0
Hi all,
Like in C++, can i create any classes and objects for those classes in VB 6.0? if it is possible, could you provide me a sample example?
Thanks:
raghunadhs.
-
Sep 21st, 2007, 10:21 AM
#2
Re: how can i use classes in VB 6.0
General idea is still the same.
Here is what you do:
- add class module to your project and name it clsEmployee
- add two textboxes to your form (txtFName and txtLName) and two buttons
- copy and paste code to appropriate object's code editor and run it:
Code:
'class module:
Option Explicit
Dim sFirstName As String
Dim sLastName As String
Public Property Get FirstName() As String
FirstName = sFirstName
End Property
Public Property Let FirstName(ByVal vNewValue As String)
sFirstName = vNewValue
End Property
Public Property Get LastName() As String
LastName = sLastName
End Property
Public Property Let LastName(ByVal vNewValue As String)
sLastName = vNewValue
End Property
'form
Option Explicit
Dim clEmployees As Collection
Private Sub Form_Load()
'initialize collection
Set clEmployees = New Collection
End Sub
Private Sub Command1_Click()
Dim Employee As clsEmployee
'create new employee class and add new it to employees collection
Set Employee = New clsEmployee
Employee.FirstName = txtFName.Text
Employee.LastName = txtLName.Text
clEmployees.Add Employee
Set Employee = Nothing
End Sub
Private Sub Command2_Click()
Dim cls As clsEmployee
'get all employees from collection
For Each cls In clEmployees
Debug.Print cls.FirstName & " " & cls.LastName
Next cls
End Sub
Private Sub Form_Unload(Cancel As Integer)
Set clEmployees = Nothing
End Sub
-
Sep 21st, 2007, 10:24 AM
#3
Member
Re: how can i use classes in VB 6.0
Yes you can create classes and can be used in the forms also
please follow the following
1) go to project explorer and add right click add> class module
2) in the class module add the following method(since you know c++ access modifiers, and parameters are something similar or equivalant here)
Public Function Greet(ByVal strMsg As String)
Greet = "welcome " & strMsg
End Function
3) goto forms and add a button and paste the following code there
Private Sub Command1_Click()
Dim c As New Class1
MsgBox c.Greet("sara")
End Sub
note i did not change the default names visual studio 6 assigned
-
Sep 21st, 2007, 10:47 AM
#4
Thread Starter
Addicted Member
Re: how can i use classes in VB 6.0
Hi RhinoBull,
i am sorry to say i could not be able to run your code, i think there may be any typing mistake, or i may need some instructions from you to run it.
and i am getting confused with the calas name clsEmployee and clEmployees.please clarify it. could you please explain what is the Collection? what are the Get and Let methods? ( i am new to these concepts...)
- add class module to your project and name it clsEmployee
Dim clEmployees As Collection 'it is differing from the class name.
Private Sub Form_Load()
'initialize collection
Set clEmployees = New Collection 'Why should i initialize it?
End Sub
Private Sub Command1_Click()
Dim Employee As clsEmployee 'this is different from collection's name...
.....
......
clEmployees.Add Employee
Set Employee = Nothing
End Sub
Thanks:
regards:
raghunadhs.
-
Sep 21st, 2007, 11:06 AM
#5
Thread Starter
Addicted Member
Re: how can i use classes in VB 6.0
[QUOTE=pons]
Hi Pons,
Thanks, it is working. i think in the same manner, i can develope everything regarding to the classes.
Thanks:
regards:
raghunadhs.
-
Sep 21st, 2007, 11:13 AM
#6
Member
Re: how can i use classes in VB 6.0
if solved can you please mark it as resolved
thanks
-
Sep 21st, 2007, 12:42 PM
#7
Re: how can i use classes in VB 6.0
@raghunadhs:
collection and class are two different type of objects - one (the class itself) is a representation of one employee (in my example) and another is collection of all employees (as an entire group).
They cannot carry the same names or your program will get "confused".
In my examle clsEmlpoyee is an object representing Employee class and clEmployees is generic collection that will hold each new instance of an emlpoyee.
Both objects can be named anything you want really.
-
Sep 23rd, 2007, 12:27 PM
#8
Thread Starter
Addicted Member
Re: how can i use classes in VB 6.0
Thanks RhinoBull,
now i got clarity...
thanks:
regards:
raghunadhs
-
Sep 23rd, 2007, 06:04 PM
#9
Re: how can i use classes in VB 6.0
Originally Posted by raghunadhs
now i got clarity...
I'm glad you did.
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
|