Creating objects at runtime 
Author Message
 Creating objects at runtime

Hello

I have an abstract class called Equities and two other classes called Bonds
and Mutuals that derive from Equities.

I first declare the object of type Equities and when they click on an option
button I return an object of type Bonds or Mutuals. I kown this can be
achieved using a select case and return an object of type Bonds or Mutuals.
If I had ten different types of objects, I think this very ugly select case.

Is there another way of achieving this without the select case, in other
words given an ID I get a string and then invoke it to create the object
with the proper type.

TIA

Yaz



Thu, 26 Aug 2004 20:40:20 GMT  
 Creating objects at runtime
MSDN and the help in VS.NET covers this very tourough,
(Don't know the links)

It's something in the style of controls.create or controls.add or dim LL as
new controls.<control>

You'll see,
Hope I directed you properly,
Kevin C.


Quote:
> Hello

> I have an abstract class called Equities and two other classes called
Bonds
> and Mutuals that derive from Equities.

> I first declare the object of type Equities and when they click on an
option
> button I return an object of type Bonds or Mutuals. I kown this can be
> achieved using a select case and return an object of type Bonds or
Mutuals.
> If I had ten different types of objects, I think this very ugly select
case.

> Is there another way of achieving this without the select case, in other
> words given an ID I get a string and then invoke it to create the object
> with the proper type.

> TIA

> Yaz



Thu, 26 Aug 2004 23:56:29 GMT  
 Creating objects at runtime

Hello

I am answering my own question, this could be achieved using the following:

Dim hdlSample As ObjectHandle
Dim myExtenderInterface As Equities
hdlSample = Activator.CreateInstance("SampleAssembly",
"samplenamespace.Bonds", True)

myExtenderInterface = CType(hdlSample.Unwrap(), IMyExtenderInterface)
Console.WriteLine(myExtenderInterface.SampleMethod("Bill"))

Is there a better way.

TIA

Yaz


Quote:
> Hello

> I have an abstract class called Equities and two other classes called
Bonds
> and Mutuals that derive from Equities.

> I first declare the object of type Equities and when they click on an
option
> button I return an object of type Bonds or Mutuals. I kown this can be
> achieved using a select case and return an object of type Bonds or
Mutuals.
> If I had ten different types of objects, I think this very ugly select
case.

> Is there another way of achieving this without the select case, in other
> words given an ID I get a string and then invoke it to create the object
> with the proper type.

> TIA

> Yaz



Fri, 27 Aug 2004 00:46:46 GMT  
 Creating objects at runtime
Hello

It's me again answering my own question

Function CreateObjec(an as string, cn as string) as Equities
    Dim asm as [Assembly] = [Assembly].Load(an)
    Dim obj as Object = asm.CreateInstance(cn)
    Return CType(obj,Equities)
End function

This would perfomance issues wouldn't it?

TIA

Yaz


Quote:
> Hello

> I have an abstract class called Equities and two other classes called
Bonds
> and Mutuals that derive from Equities.

> I first declare the object of type Equities and when they click on an
option
> button I return an object of type Bonds or Mutuals. I kown this can be
> achieved using a select case and return an object of type Bonds or
Mutuals.
> If I had ten different types of objects, I think this very ugly select
case.

> Is there another way of achieving this without the select case, in other
> words given an ID I get a string and then invoke it to create the object
> with the proper type.

> TIA

> Yaz



Fri, 27 Aug 2004 00:58:10 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Creating objects in runtime using the mouse??

2. Create Object at Runtime Help

3. Newbie Q: Creating objects at runtime

4. creating objects at runtime in VB 5.0

5. Creating Objects at Runtime

6. Creating object during RunTime with Visual Basic 5.0 CCE

7. Create Object At RunTime (Error) adding news propertys dinamically

8. How to create object at runtime ?

9. Creating Objects Durring RunTime

10. Creating Object at Runtime - Not Form

11. Creating Objects Durring RunTime

12. Why Can't I Figure This Out - Creating Objects During Runtime

 

 
Powered by phpBB® Forum Software