Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sub to create object of type passed by caller in VB.NET

Tags:

vb.net

I'm having trouble creating a sub that can create objects of a variable type on the fly. Here's an example of what I'm trying to achieve:

class systemSettings
    'some properties
end class

Class fireSystemSettings
    inherits systemSettings
    'some additional properties
end class

Class windSystemSettings
    inherits systemSettings
    'some additional properties
end class

sub createSystem(systemType as Type, arg1 as object, arg2 as object)
    Dim newSystem as New systemType(arg1, arg2)
    systemCollection.add(newSystem)
end sub

I can't get it to work. I've done a fair bit of research, and looked at generic types, reflection, and other tools, but I'm having trouble determining how best to tackle this problem.

like image 370
Michael Avatar asked Dec 01 '11 04:12

Michael


1 Answers

You're looking for Activator.CreateInstance(systemType)

like image 87
SLaks Avatar answered Oct 17 '22 07:10

SLaks