Declaring objects as same as...

Briinnok

Member
Joined
Feb 6, 2008
Messages
8
Programming Experience
10+
I am developing a project that has many class types, all derived from a common base class. I want to be able to, on the fly, create objects of these classes by using some string variable to tell me which one to create.

In other words,

clsObject1, clsObject2, clsObject3 are all derived from clsObject.

I read from a file, "Object1" and "Object3".

I want to be able to Dim Object as new clsObject1 Object2 as new clsObject3, simply because that's what the file said to do.

The problem is there will be so many clsObjects, with more being added later, so I don't want to have a switch that checks for each object manually. I want this to be easily expanded, using some variable within clsObject to tell me which one to create.

My idea is to create a copy of each of these objects, place them in a Collection with the string key, and dim a new object as the same type as whatever comes out of the Collection by the key.

Is there a way to do this?

Is this post confusing as heck? I'm having a hard time expressing myself here. Sorry.
 
You can do what you're asking for but it's not going to be as easy as you think. Your title uses the term "declaring" but declaring is an instruction to the compiler. If you don't know the type of your objects until run time then you can't tell the compiler anything about them.

You can use reflection to do what you're asking for. You can create a Type object from a String containing the name of your class, then use that Type to create instances of the corresponding type. The best you'll be able to do, though, is cast them as your common base type and treat them as instances of that type. If you want to access specific members of each derived type then you'll have to do that via reflection too. As I said, if you don't know what the type is until run time you can't tell the compiler that the object is a particular type.

The thing for you to do first is to read about the Type class in the MSDN Library and then do some more reading on reflection.
 
Is there a way to make a duplicate copy of an existing instance of a class programmatically? There has to be some way to do this, otherwise the creation of third party plug-ins for a program would be impossible. That is essentially what I am doing.

I am creating a hacking sim, along the lines of Uplink. I loved the game, and have several ideas about how to improve on it.

What I am trying to do here is allow for future creation of third party hacking programs and server types to be added at a later date, without a redo of the original code.
 
Plug-in architectures are achieved using reflection, which I've already described in simple terms. If you search the Web for plugin architecture and similar terms you'll find more than one example.

Basically, your app defines an interface that all plug-in components must implement. At run time your app will scan one or more folders for assemblies, load them and then use reflection to search for types that implement that interface. You can then treat each plug-in as an instance of your interface. You access the members declared in your interface and then each plug-in's custom implementation of those members will be executed.

This concept is actually very familiar to any reasonably experienced computer user. I'm sure you've heard of USB. It's a well-known, clearly defined interface. Your computer knows what a USB device is because it knows the definition of the USB interface. You can then plug any USB device at all into the USB ports on your computer. It doesn't matter what the device does as long as it implements the USB interface. The computer will send standard USB instructions and receive standard USB replies so it's happy, but each device is at liberty to act on those instructions and create those replies in any way it pleases.
 
I think I'm going to have to look further into this reflection stuff. I'm so used to old school c++ and vb, this .net stuff is taking a bit to learn.

I apologize if I'm making you repeat what might seem to you obvious statements. I think you've pointed me on the right path, though. I appreciate your help.
 
Back
Top