class types in vb.net

  • Thread starter Thread starter Marius
  • Start date Start date
M

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.
 
Something like this?


Public Function Foo(ByVal theClass As Object) As Type
Return theClass.GetType()
End Function
 
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()
 
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.
 
Marius said:
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.
 
Back
Top