Home > Archive > Visual Basic > March 2004 > TypeOf??
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Stephen J Bement 2004-03-29, 1:30 pm |
| Thanks, I take it that inheritance/polymorphism is implemented through sub
classing then?
Is there a way to tell if a class implements a certain interface?
Class Bubba
Implements IBarfly
Implements IPoet
Class Gwen
Implements IBarfly
Implements IKareokeist
Therefore:
Collection.Add New Gwen
Collection.Add New Bubba
For Each IBarfly in Collection
IBarfly.Drink 3
Next IBarfly
For Each Item in Collection
If Item = IPoet Then
Item.ReciteLimerick
ElseIf Item = IKareokeist Then
Item.Sing("Yellow Rose Of Texas")
End If
Next Item
--
Semper Fi,
Red
Please post to newsgroup only
| |
| Larry Serflaten 2004-03-29, 4:30 pm |
|
"Stephen J Bement" <spam@null.net> wrote
> Thanks, I take it that inheritance/polymorphism is implemented through sub
> classing then?
I haven't bothered to look exactly how it is done, as long as it is
reliable, I can live with it being 'Black Box'...
> Is there a way to tell if a class implements a certain interface?
Dim Poet as IPoet
Dim Singer as IKareokeist
> For Each Item in Collection
> If TypeOf Item Is IPoet Then
Set Poet = Item
> Poet.ReciteLimerick
> ElseIf TypeOf Item Is IKareokeist Then
Set Singer = Item
> Singer.Sing("Yellow Rose Of Texas")
> End If
> Next Item
If you want to access the methods of an interface
then you have to declare a variable of that type of
interface.
LFS
| |
| Stephen J Bement 2004-03-29, 5:30 pm |
| But how do I tell if an object conforms to an interface?
If I were to:
Dim Singer as IKareokeist
Set Singer = Bubba
I would expect an error either during the assignment or at least when I try
to call Buuba.Sing() since Bubba::IBarfly,IPoet
"Larry Serflaten" <serflaten@usinternet.com> wrote in message
news:eZS3dEdFEHA.3448@TK2MSFTNGP09.phx.gbl...
>
> "Stephen J Bement" <spam@null.net> wrote
sub[color=darkred]
>
> I haven't bothered to look exactly how it is done, as long as it is
> reliable, I can live with it being 'Black Box'...
>
>
>
> Dim Poet as IPoet
> Dim Singer as IKareokeist
>
> Set Poet = Item
> Set Singer = Item
>
>
> If you want to access the methods of an interface
> then you have to declare a variable of that type of
> interface.
>
> LFS
>
>
>
| |
| Larry Serflaten 2004-03-29, 5:30 pm |
|
"Stephen J Bement" <spam@null.net> wrote in message
> But how do I tell if an object conforms to an interface?
By testing it with TypeOf as was shown.
>
> If I were to:
>
> Dim Singer as IKareokeist
>
> Set Singer = Bubba
>
> I would expect an error either during the assignment or at least when I try
> to call Buuba.Sing() since Bubba::IBarfly,IPoet
It will error....
[color=darkred]
If it passes the TypeOf test, then you can use it, otherwise
it does not support that interface, and will error.
LFS
| |
| Ken Halter 2004-03-29, 5:30 pm |
| Did you try it? iirc, you should be getting a Type Mismatch error when the
'Set' runs. You'll definetly get a "procedure not found" type error if
there's no such procedure.
....and, actually, your code should look more like...
> Dim DrunkIdiot as IKareokeist
>
> Set DrunkIdiot = SlobberingAndFallingDown
--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
Please keep all discussions in the groups..
"Stephen J Bement" <spam@null.net> wrote in message
news:uNwR5IdFEHA.4008@TK2MSFTNGP10.phx.gbl...
> But how do I tell if an object conforms to an interface?
>
> If I were to:
>
> Dim Singer as IKareokeist
>
> Set Singer = Bubba
>
> I would expect an error either during the assignment or at least when I
try
> to call Buuba.Sing() since Bubba::IBarfly,IPoet
>
| |
| Stephen J Bement 2004-03-29, 6:34 pm |
| Thanks alot for you help.
"Larry Serflaten" <serflaten@usinternet.com> wrote in message
news:OOIa6MdFEHA.3764@TK2MSFTNGP12.phx.gbl...
>
> "Stephen J Bement" <spam@null.net> wrote in message
>
> By testing it with TypeOf as was shown.
>
try[color=darkred]
>
> It will error....
>
>
>
>
> If it passes the TypeOf test, then you can use it, otherwise
> it does not support that interface, and will error.
>
> LFS
>
>
|
|
|
|
|