Home All Groups Group Topic Archive Search About

"Type" in parameter list

Author
29 Mar 2005 1:47 PM
Art
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

Author
29 Mar 2005 1:57 PM
Herfried K. Wagner [MVP]
"Art" <A**@discussions.microsoft.com> schrieb:
> 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.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
29 Mar 2005 2:49 PM
Art
Herfried,

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

          Art

Show quoteHide quote
"Herfried K. Wagner [MVP]" wrote:

> "Art" <A**@discussions.microsoft.com> schrieb:
> > 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.
>
> --
>  M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
>  V B   <URL:http://classicvb.org/petition/>
>
>
Author
29 Mar 2005 4:10 PM
Herfried K. Wagner [MVP]
"Art" <A**@discussions.microsoft.com> schrieb:
> 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
///

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
29 Mar 2005 2:56 PM
Cor Ligthert
Art,

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

Cor
Author
29 Mar 2005 3:13 PM
Art
Cor,

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

           Art

Show quoteHide quote
"Cor Ligthert" wrote:

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