Valhalla Legends Forums Archive | Visual Basic Programming | [VB6] Class Module with methods that have methods... How do you do this?

AuthorMessageTime
bethra
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:
[code]
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
[/code]

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

Thanks and have a merry Christmas... or whatever you celebrate.
December 25, 2005, 12:16 AM
Topaz
You could probably use Optional for the second parameter
December 25, 2005, 1:53 AM
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:
[code]
Public Class As New clsMain
[/code]


In clsEvents code:
[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
[/code]


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

And in the form code:
[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
[/code]

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!
December 26, 2005, 5:03 PM
LoRd
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.

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

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

End Sub[/code]
December 26, 2005, 5:15 PM
Ringo
Oh, i was miles off what he was trying to do then :P
December 26, 2005, 5:20 PM

Search