Results 1 to 9 of 9

Thread: [RESOLVED] how can i use classes in VB 6.0

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    167

    Resolved [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.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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

  3. #3

    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

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    167

    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.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    167

    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.

  6. #6

    Re: how can i use classes in VB 6.0

    if solved can you please mark it as resolved

    thanks

  7. #7
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    167

    Re: how can i use classes in VB 6.0

    Thanks RhinoBull,
    now i got clarity...

    thanks:
    regards:
    raghunadhs

  9. #9

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