From: Steffen Leppert on
Hello all!

I noticed that when I use MSXML2, I can easily cast an IXMLDOMNode to an
IXMLDOMElement and vice versa.

Both are classes, right?

Does anybody know how this works under the hood? I am not aware that I
can do the same with my own classes.

Steffen
From: ralph on
On Thu, 29 Jul 2010 07:07:12 +0200, Steffen Leppert
<st.leppert(a)gmx.de> wrote:

>Hello all!
>
>I noticed that when I use MSXML2, I can easily cast an IXMLDOMNode to an
>IXMLDOMElement and vice versa.
>
>Both are classes, right?
>
>Does anybody know how this works under the hood? I am not aware that I
>can do the same with my own classes.
>

Not familar with "casting" in VB6.

Perhaps you can show the construct you use to perform casting.

-ralph
From: Tom Shelton on
Steffen Leppert formulated on Wednesday :
> Hello all!
>
> I noticed that when I use MSXML2, I can easily cast an IXMLDOMNode to an
> IXMLDOMElement and vice versa.
>
> Both are classes, right?
>

Actually, IXMLDOMNode and IXMLDOMElement are interfaces... An
interface, is like a class - but has not implementation. It describes
the contract of an object.

Obviously, you are being returned an object of some class - but that
actual type is unknown to you. You simply are accessing it through the
IXMLDOMNode and IXMLDOMElement interfaces.

A COM object can implement and expose any number of interfaces...

> Does anybody know how this works under the hood?

Basically, you are getting a pointer to the interface on the class.
Basically, a pointer into the vtable of the class that implements these
interfaces...

> I am not aware that I can do the same with my own classes.

sure you can:


IInterface1:

Public Function DoCoolStuff1() As Integer
' NO OP - this is an interface
End Function

IInterface2:
Public Function DoCoolStuff2() As Integer
' NO OP - this is an interface
End Function

AClass:
Implements IInterface1
Implements IInterface2

Public Function MyFunction() As Integer
MyFunction = 15
End Function

Private Function IInterface1_DoCoolStuff1() As Integer
IInterface1_DoCoolStuff1 = 5
End Function

Private Function IInterface2_DoCoolStuff2() As Integer
IInterface2_DoCoolStuff2 = 10
End Function


In form1:
Private Sub Form_Load()
Dim a As AClass
Dim i1 As IInterface1
Dim i2 As IInterface2

Set a = New AClass ' create a class instance
Set i1 = a ' cast to IInterface1 so we can call it's
methods
Set i2 = a ' cast to IInterface2 so we can call it's
methods

MsgBox "MyFunction = " & a.MyFunction
MsgBox "DoCoolStuff1 = " & i1.DoCoolStuff1
MsgBox "DoCoolStuff2 = " & i2.DoCoolStuff2

End Sub

HTH

--
Tom Shelton


From: Steffen Leppert on
Hello Tom,

where do you define IInterface? In a class or in a module?

Steffen

Am 29.07.2010 09:21, schrieb Tom Shelton:
> Steffen Leppert formulated on Wednesday :
>> Hello all!
>>
>> I noticed that when I use MSXML2, I can easily cast an IXMLDOMNode to
>> an IXMLDOMElement and vice versa.
>>
>> Both are classes, right?
>>
>
> Actually, IXMLDOMNode and IXMLDOMElement are interfaces... An interface,
> is like a class - but has not implementation. It describes the contract
> of an object.
>
> Obviously, you are being returned an object of some class - but that
> actual type is unknown to you. You simply are accessing it through the
> IXMLDOMNode and IXMLDOMElement interfaces.
>
> A COM object can implement and expose any number of interfaces...
>
>> Does anybody know how this works under the hood?
>
> Basically, you are getting a pointer to the interface on the class.
> Basically, a pointer into the vtable of the class that implements these
> interfaces...
>
>> I am not aware that I can do the same with my own classes.
>
> sure you can:
>
>
> IInterface1:
>
> Public Function DoCoolStuff1() As Integer
> ' NO OP - this is an interface
> End Function
>
> IInterface2:
> Public Function DoCoolStuff2() As Integer
> ' NO OP - this is an interface
> End Function
>
> AClass:
> Implements IInterface1
> Implements IInterface2
>
> Public Function MyFunction() As Integer
> MyFunction = 15
> End Function
>
> Private Function IInterface1_DoCoolStuff1() As Integer
> IInterface1_DoCoolStuff1 = 5
> End Function
>
> Private Function IInterface2_DoCoolStuff2() As Integer
> IInterface2_DoCoolStuff2 = 10
> End Function
>
>
> In form1:
> Private Sub Form_Load()
> Dim a As AClass
> Dim i1 As IInterface1
> Dim i2 As IInterface2
>
> Set a = New AClass ' create a class instance
> Set i1 = a ' cast to IInterface1 so we can call it's methods
> Set i2 = a ' cast to IInterface2 so we can call it's methods
>
> MsgBox "MyFunction = " & a.MyFunction
> MsgBox "DoCoolStuff1 = " & i1.DoCoolStuff1
> MsgBox "DoCoolStuff2 = " & i2.DoCoolStuff2
>
> End Sub
>
> HTH
>

From: Dee Earley on
On 29/07/2010 11:19, Steffen Leppert wrote:
> Hello Tom,
>
> where do you define IInterface? In a class or in a module?

A class.

VB6 doesn't allow creation of explicit interfaces.
They are just classes that don't actually do anything.
(If exposed via COM, make them public not creatable)

--
Dee Earley (dee.earley(a)icode.co.uk)
i-Catcher Development Team

iCode Systems

(Replies direct to my email address will be ignored.
Please reply to the group.)