• Welcome to Valhalla Legends Archive.
 

[VB6] Class Module with methods that have methods... How do you do this?

Started by Sorc.Polgara, December 24, 2005, 06:16 PM

Previous topic - Next topic

Sorc.Polgara

Let me describe what I'm asking to the best I can.

Let's say I have a Class Module named "SomeClass"

"SomeClass" has a public method/function named "SomeMethod" that returns a String.

"SomeMethod" has two methods/functions named "DoesSomething1" that takes an Integer as a parameter and "DoesSomething2" that has no parameters.  Both return a String.

Using it would go something like:

Dim cls As SomeClass
Dim i As Integer
Dim str1 As String
Dim str2 As String

i = 2
str1 = cls.SomeMethod.DoesSomething1(i)
str2 = cls.SomeMethod.DoesSomething2


First off, this is possible to do this in VB6 right?

Thanks and have a merry Christmas... or whatever you celebrate.

topaz

RLY...?

Ringo

I sort of follow what you want to do, but im not 100% sure.

You could always access multiple class's from one if you wanted, that would probly work.

Try messing around with this:
You need a form, a module and 2 class's.
Name the 2 class's clsMain and clsEvents

In the module code:

Public Class As New clsMain



In clsEvents code:

Public Event Event1(ByRef StrString As String)

Public Sub DoEvent(ByVal StrReturn As String)
    RaiseEvent Event1(StrReturn)
End Sub

Public Sub DoEvent2(ByRef StrReturn As String)
    StrReturn = "Event return"
    RaiseEvent Event1(StrReturn)
End Sub

Public Function GetStr() As String
    GetStr = "Return String"
End Function



In clsMain code:

Public WithEvents SomeMethod As clsEvents
Public SomeOtherMethod As New clsEvents
Private Sub SomeMethod_Event1(StrString As String)
    MsgBox "[Class Event] " & StrString
End Sub


And in the form code:

Private Sub Form_Load()
    Set Class.SomeMethod = New clsEvents
End Sub

Private Sub Command1_Click()
    'cause a event to be raised and have the string returned
    Dim rStr As String
    Class.SomeMethod.DoEvent2 rStr
    MsgBox rStr
   
    'hand a string to be raised in the event
    Call Class.SomeMethod.DoEvent("StrString22")
   
    'have a string returned from clsEvents via clsMain with out useing events
    MsgBox Class.SomeOtherMethod.GetStr
    'or do it from your events object
    MsgBox Class.SomeMethod.GetStr
End Sub


VB isnt to fussy about which way you do it, but for what i think your trying to do, best not use events. :)

hope it helps!

Eric

This could be easily implimented using function overloading.  Too bad Visual Basic doesn't support the use of function overloading!  You'll either have to create two seperate functions or use the Optional keyword as previously mentioned.

Private Function DoesSomething(Optional i as Integer = 0) As String

    If (i <> 0) Then
        ' DoesSomething1
    Else
        ' DoesSomething2
    End If

End Sub

Ringo