"Type" in parameter list

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

Is there a way for me to vary the Type of a parameter in a method in a
class? Ideally I'd like to have a variable that I can use, such as:

Public Sub MyMethod (ByVal mParm as mType)

where mType can vary.

Art
 
Art said:
Is there a way for me to vary the Type of a parameter in a method in a
class? Ideally I'd like to have a variable that I can use, such as:

Public Sub MyMethod (ByVal mParm as mType)

where mType can vary.

\\\
Public Interaface IFoo
Sub Goo()
End Interface

Public Class BlaFoo
Implements IFoo

Public Sub Goo() Implements IFoo.Goo
...
End Sub
End Class

Public class BazFoo
Implements IFoo

...
End Class
..
..
..
Public Sub DoSomething(ByVal f As IFoo)
f.Goo()
End Sub
..
..
..
DoSomething(New BlaFoo())
DoSomething(New BazFoo())
///

The sample above shows how to use an interface (or a common base class) to
limit the types of objects passed to the method to the parameter's type and
all its subtypes.

By changing the type of the method's parameter to 'Object', you change it to
the most generic type. You can pass objects of any type to this parameter.
 
Herfried,

Thanks for this also. This one will take me a little time to try out.

Art
 
Art,

Although I don't like it pass as object and get it out the method by using
the typeof

Cor
 
Cor,

Yeah, I don't like it either -- that's why I was hoping for something a
little better. Thanks as always!

Art
 
Art said:
Thanks for this also. This one will take me a little time to try out.

In addition to my previous reply, you may want to use 'TypeOf' to determine
the type of the object passed to the parameter:

\\\
Public Sub ClearControl(ByVal Control As Control)
If TypeOf Control Is TextBox Then
DirectCast(Control, TextBox).Text = ""
...
ElseIf TypeOf Control Is ... Then
...
Else...
...
...
End If
End Sub
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top