is there a way to pass an interface type to a method? For example (this is not the best example, as the architecture of the example is obviously not so good, but for simplicity and fun sake): I want to be able to do something like so: I have a "Has" method
that would be defined like so:
... Public Function Has(ByVal InterfaceType As System.Type) As Boolean ...
that method works fine (see the code below for a full example of the method), and I would like to use the above method like so: (similar to the queryinterface method in the com world with out the out object)
... If Not (Family.Has(IMom)) Then ...
however the code above produces the following error:
D:\ConsoleApplication1\Module1.vb(4): 'IMom' is a type and cannot be used as an expression.
(see the code below for the full example)
Module Module1
Sub Main()
Dim Family As FamilyCollection = New FamilyCollection("Smith")
If Not (Family.Has(IMom)) Then
Console.WriteLine("The {0} family has no Mom", Family.FamilyName)
End If
End Sub
End Module
Public Interface IMom
ReadOnly Property IsGoodCook() As Boolean
End Interface
Public Class FamilyCollection
Inherits System.Collections.CollectionBase
Private _FamilyName As String
Public Sub New(ByVal FamilyName As String)
_FamilyName = FamilyName
End Sub
Public ReadOnly Property FamilyName() As String
Get
Return _FamilyName
End Get
End Property
Public Function Add(ByVal FamilyMember As Object)
List.Add(FamilyMember)
End Function
Public Function Has(ByVal InterfaceType As System.Type) As Boolean
Dim Result As Boolean = False
For Each Item As Object In Me.List
If Item Is InterfaceType Then
Result = True
Exit For
End If
Next
Return Result
End Function
End Class
"Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).
I would like to do that, but the interfaces will not be known by the FamilyCollection class, so I need to pass the interface type. I do not want other developers to have do any code in the family class - plus it would be a pain to code the same function over
and over again, for example if I used the above code I would have to a HasMom function, a HasDad function, etc. Think of the Family class as static. I want it as a single place that you get the Mom instance, the Dad instance, the pet instance etc.
close... it compiles but does not work as desired. The Has function returns false even when a class that implements the IMom interface for example if I take the code above and add the class:
Public Class Jane
Implements IMom
Public ReadOnly Property IsGoodCook() As Boolean Implements IMom.IsGoodCook
Get
Return True
End Get
End Property
End Class
and change the module to be:
Module Module1
Sub Main()
Dim Family As FamilyCollection = New FamilyCollection("Smith")
Family.Add(New Jane)
If Not (Family.Has(GetType(IMom))) Then
Console.WriteLine("The {0} family has no Mom", Family.FamilyName)
Else
Console.WriteLine("The {0} family has a Mom", Family.FamilyName)
End If
End Sub
End Module
ok I have got it.. and it is a little cleaner. here is all the code, but if anyone could answer my last question after the code) - please respond: I changed the methods to be more politically correct :)
Module Module1
Sub Main()
Dim Family As FamilyCollection = New FamilyCollection("Smith")
Family.Add(New Jane)
Dim Mom As IMom = Family("IMom")
If Mom Is Nothing Then
Console.WriteLine("The {0} family has no Mom", Family.FamilyName)
Else
Console.WriteLine("The {0} family has a Mom", Family.FamilyName)
End If
Dim Dad As IDad = Family("IDad")
If Dad Is Nothing Then
Console.WriteLine("The {0} family has no Dad", Family.FamilyName)
Else
Console.WriteLine("The {0} family has a Dad", Family.FamilyName)
End If
End Sub
End Module
Public Interface IDad
ReadOnly Property IsUncle() As Boolean
End Interface
Public Interface IMom
ReadOnly Property IsAunt() As Boolean
End Interface
Public Class Bill
Implements IDad
Public ReadOnly Property IsUncle() As Boolean Implements IDad.IsUncle
Get
Return True
End Get
End Property
End Class
Public Class Jane
Implements IMom
Public ReadOnly Property IsAunt() As Boolean Implements IMom.IsAunt
Get
Return True
End Get
End Property
End Class
Public Class FamilyCollection
Inherits System.Collections.CollectionBase
Private _FamilyName As String
Public Sub New(ByVal FamilyName As String)
Me._FamilyName = FamilyName
End Sub
Public ReadOnly Property FamilyName() As String
Get
Return _FamilyName
End Get
End Property
Public Function Add(ByVal FamilyMember As Object)
List.Add(FamilyMember)
End Function
Default Public ReadOnly Property Member(ByVal InterfaceName As String) As Object
Get
Dim Result As Object = Nothing
For Each Item As Object In Me.List
If Not (Item.GetType.GetInterface(InterfaceName, True) Is Nothing) Then
Result = Item
Exit For
End If
Next
Return Result
End Get
End Property
End Class
my question is: Is the following line of code effecient? Or is there a better way to implement this:
If Not (Item.GetType.GetInterface(InterfaceName, True) Is Nothing) Then
lcarriere
Member
50 Points
10 Posts
passing interface type
Dec 22, 2004 05:31 PM|LINK
Module Module1 Sub Main() Dim Family As FamilyCollection = New FamilyCollection("Smith") If Not (Family.Has(IMom)) Then Console.WriteLine("The {0} family has no Mom", Family.FamilyName) End If End Sub End Module Public Interface IMom ReadOnly Property IsGoodCook() As Boolean End Interface Public Class FamilyCollection Inherits System.Collections.CollectionBase Private _FamilyName As String Public Sub New(ByVal FamilyName As String) _FamilyName = FamilyName End Sub Public ReadOnly Property FamilyName() As String Get Return _FamilyName End Get End Property Public Function Add(ByVal FamilyMember As Object) List.Add(FamilyMember) End Function Public Function Has(ByVal InterfaceType As System.Type) As Boolean Dim Result As Boolean = False For Each Item As Object In Me.List If Item Is InterfaceType Then Result = True Exit For End If Next Return Result End Function End Classbmains
All-Star
29116 Points
5886 Posts
MVP
Re: passing interface type
Dec 22, 2004 06:24 PM|LINK
"Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).
rsmoke21
Contributor
3931 Points
792 Posts
Re: passing interface type
Dec 22, 2004 07:18 PM|LINK
Dim l As New ArrayList If TypeOf l Is IList Then Response.Write("Yeah!") End Iflcarriere
Member
50 Points
10 Posts
Re: passing interface type
Dec 22, 2004 07:44 PM|LINK
lcarriere
Member
50 Points
10 Posts
Re: passing interface type
Dec 22, 2004 07:49 PM|LINK
Public Class Jane Implements IMom Public ReadOnly Property IsGoodCook() As Boolean Implements IMom.IsGoodCook Get Return True End Get End Property End Classand change the module to be:Module Module1 Sub Main() Dim Family As FamilyCollection = New FamilyCollection("Smith") Family.Add(New Jane) If Not (Family.Has(GetType(IMom))) Then Console.WriteLine("The {0} family has no Mom", Family.FamilyName) Else Console.WriteLine("The {0} family has a Mom", Family.FamilyName) End If End Sub End ModuleThe "Has" function still returns False?lcarriere
Member
50 Points
10 Posts
Re: passing interface type
Dec 22, 2004 09:09 PM|LINK
Module Module1 Sub Main() Dim Family As FamilyCollection = New FamilyCollection("Smith") Family.Add(New Jane) Dim Mom As IMom = Family("IMom") If Mom Is Nothing Then Console.WriteLine("The {0} family has no Mom", Family.FamilyName) Else Console.WriteLine("The {0} family has a Mom", Family.FamilyName) End If Dim Dad As IDad = Family("IDad") If Dad Is Nothing Then Console.WriteLine("The {0} family has no Dad", Family.FamilyName) Else Console.WriteLine("The {0} family has a Dad", Family.FamilyName) End If End Sub End Module Public Interface IDad ReadOnly Property IsUncle() As Boolean End Interface Public Interface IMom ReadOnly Property IsAunt() As Boolean End Interface Public Class Bill Implements IDad Public ReadOnly Property IsUncle() As Boolean Implements IDad.IsUncle Get Return True End Get End Property End Class Public Class Jane Implements IMom Public ReadOnly Property IsAunt() As Boolean Implements IMom.IsAunt Get Return True End Get End Property End Class Public Class FamilyCollection Inherits System.Collections.CollectionBase Private _FamilyName As String Public Sub New(ByVal FamilyName As String) Me._FamilyName = FamilyName End Sub Public ReadOnly Property FamilyName() As String Get Return _FamilyName End Get End Property Public Function Add(ByVal FamilyMember As Object) List.Add(FamilyMember) End Function Default Public ReadOnly Property Member(ByVal InterfaceName As String) As Object Get Dim Result As Object = Nothing For Each Item As Object In Me.List If Not (Item.GetType.GetInterface(InterfaceName, True) Is Nothing) Then Result = Item Exit For End If Next Return Result End Get End Property End Classmy question is: Is the following line of code effecient? Or is there a better way to implement this: