Home All Groups Group Topic Archive Search About

class types in vb.net

Author
18 Dec 2006 10:04 AM
Marius
I am a Delphi programmer switching to vb.net. In Delphi you could
define a metaclasstype like

type MetaClassName = class of TSomeClass;

and you could use this type as a function result. This way you could
get as a function result the type of a class instead of an instance of
this class.

My question is, is there an equivalent in vb.net? And if so, what is
the syntax.

------
Marius

Author
18 Dec 2006 11:44 AM
Robinson
Something like this?


Public Function Foo(ByVal theClass As Object) As Type
    Return theClass.GetType()
End Function
Author
18 Dec 2006 12:00 PM
Stephany Young
It would be:

  Dim MetaClassName As Type = Type.GetType(TSomeClass)

If you had a variable that was an instance of something then it would be:

  Dim MetaClassName As Type = SomeInstance.GetType()


Show quoteHide quote
"Marius" <mariusdev***@gmail.com> wrote in message
news:1166436292.766818.170830@n67g2000cwd.googlegroups.com...
>I am a Delphi programmer switching to vb.net. In Delphi you could
> define a metaclasstype like
>
> type MetaClassName = class of TSomeClass;
>
> and you could use this type as a function result. This way you could
> get as a function result the type of a class instead of an instance of
> this class.
>
> My question is, is there an equivalent in vb.net? And if so, what is
> the syntax.
>
> ------
> Marius
>
Author
18 Dec 2006 3:12 PM
Jay B. Harlow
Marius,
In addition to the other comments:

Consider using:

> type MetaClassName = class of TSomeClass;
    Dim MetaClassName As Type = GetType(TSomeClass)

Be mindful of the GetType keyword, and the Type.GetType function.

The GetType keyword accepts a type identifier & will cause a compile error
if you give a non-existent type.

    Dim MetaClassName As Type = GetType(TSomeClass)

The Type.GetType function accepts a string & will cause a runtime error if
you give it a non-existent type. Generally its safest to give a qualified
name to Type.GetType that includes the namespace along with the assembly.

    Dim MetaClassName As Type = GetType("SomeNamespace.TSomeClass,
SomeAssembly")


For types within my assembly or assemblies that I know I have explicitly
referenced I will use the GetType keyword so as to ensure compile time
errors.

For types that I want to dynamically load, for example plugins listed in the
app/web config, I will use the Type.GetType function.

--
Hope this helps
Jay B. Harlow
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Show quoteHide quote
"Marius" <mariusdev***@gmail.com> wrote in message
news:1166436292.766818.170830@n67g2000cwd.googlegroups.com...
>I am a Delphi programmer switching to vb.net. In Delphi you could
> define a metaclasstype like
>
> type MetaClassName = class of TSomeClass;
>
> and you could use this type as a function result. This way you could
> get as a function result the type of a class instead of an instance of
> this class.
>
> My question is, is there an equivalent in vb.net? And if so, what is
> the syntax.
>
> ------
> Marius
>
Author
18 Dec 2006 4:06 PM
Herfried K. Wagner [MVP]
"Marius" <mariusdev***@gmail.com> schrieb:
>I am a Delphi programmer switching to vb.net. In Delphi you could
> define a metaclasstype like
>
> type MetaClassName = class of TSomeClass;
>
> and you could use this type as a function result. This way you could
> get as a function result the type of a class instead of an instance of
> this class.
>
> My question is, is there an equivalent in vb.net? And if so, what is
> the syntax.

Check out the 'System.Type' class, its members and the 'GetType' statement.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>