I recently noticed the CallByName keyword in VB6.
Since this takes a object, procedure name, "call type" and arguments array, can this be used to "fake" some types of polymorphic behavior?
I can make 2 classes, class A and B, each with the same method Foo, and do:
Dim list As New Collection
Dim instanceA As New ClassA
Dim instanceB As New ClassB
Dim current As Object
Call list.Add(instanceA)
Call list.Add(instanceB)
For Each current in list
Call CallByName(current, "methodName", vbMethod)
Next
Anyone done this before? Problems? Horrible idea or genius idea? Implications? Unintended consequences?
Why fake polymorphism? VB6 has real polymorphism in the form of interfaces:
' Interface1.cls '
Sub Foo()
End Sub
' --------------------------------------------- '
' Class1.cls '
Implements Interface1
Private Sub Interface1_Foo()
? "Hello from class 1"
End Sub
' --------------------------------------------- '
' Class2.cls '
Implements Interface1
Private Sub Interface1_Foo()
? "Hello from class 2"
End Sub
' --------------------------------------------- '
' Module1.mod '
Dim x As Interface1
Set x = New Class1
Call x.Foo()
Set x = New Class2
Call x.Foo()
Although I agree with Mr. unicorn, I can't help but point out that CallByName is also unnecessary (in this case) because you can call the method using the standard method syntax and it will result in a late-bound (i.e. not resolved at compile-time) call:
...
For Each current In list
Call current.methodName
Next
The real use of CallByName is to reference method names/properties where the name comes from a (possibly calculated) string value...an absolute abomination, in my opinion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With