473,320 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Instansitate a class by String

I think I've seen this somewhere, but I cannot remember how to initialze a
class by a string.

That is, I have a class(Component) that I want to initialize on the fly. It
is derived from a base component I created.

So basically I have.

Public class myDerivedComponent
inherits myBaseComponent

.... stuff ...

End Class

and in another code I want to have

dIm myString as String = "myDerivedComponent"

Dim myD as myBaseComponent

myD.InstanceOf (myString)

Hopefully this makes sense.

-CJ
Nov 19 '05 #1
4 1191
Nevermind found it.

For thsoe that want the answer, its in the Activator class. (System
namespace).

I'll let you know how it goes.

-CJ
"CJ Taylor" <ct*****@mortonwelding.com> wrote in message
news:u8**************@TK2MSFTNGP09.phx.gbl...
I think I've seen this somewhere, but I cannot remember how to initialze a
class by a string.

That is, I have a class(Component) that I want to initialize on the fly. It is derived from a base component I created.

So basically I have.

Public class myDerivedComponent
inherits myBaseComponent

... stuff ...

End Class

and in another code I want to have

dIm myString as String = "myDerivedComponent"

Dim myD as myBaseComponent

myD.InstanceOf (myString)

Hopefully this makes sense.

-CJ

Nov 19 '05 #2
I was looking into that, but it seemed to much since the method I was
calling was within the Current Executing Assembly.

You would have to work here to understand the weirdness of the situation.
=-)

<bu********@yahoo.com> wrote in message
news:01****************************@phx.gbl...
Well, I've done something like that in a differnt way.

I get all Assembly, Class and method on the fly and
execute it.
NOTE: to use "LoadWithPartialName" you need u'r assembly
in the GAC.

'Load the DLL
objRef =
System.Reflection.Assembly.LoadWithPartialName(str DLLName)
If objRef Is Nothing Then Throw New
Exception("Assembly : '" & strDLLName & "' could not be
loaded.")
'Instansiate the Class
objInstance = objRef.CreateInstance
(strClassName)
If objInstance Is Nothing Then Throw New
Exception("Class : '" & strClassName & "' could not be
instantiated.")
'Call the Function
CallByName(objInstance, strMethodName,
CallType.Method, Object)
-----Original Message-----
Nevermind found it.

For thsoe that want the answer, its in the Activator

class. (System
namespace).

I'll let you know how it goes.

-CJ
"CJ Taylor" <ct*****@mortonwelding.com> wrote in message
news:u8**************@TK2MSFTNGP09.phx.gbl...
I think I've seen this somewhere, but I cannot remember how to initialze a class by a string.

That is, I have a class(Component) that I want to

initialize on the fly.
It
is derived from a base component I created.

So basically I have.

Public class myDerivedComponent
inherits myBaseComponent

... stuff ...

End Class

and in another code I want to have

dIm myString as String = "myDerivedComponent"

Dim myD as myBaseComponent

myD.InstanceOf (myString)

Hopefully this makes sense.

-CJ

.

Nov 19 '05 #3
Ok, so Activator isn't working th eway I want to (I could be doing something
wrong)

Here is my current code

Public Function CreateEosBase(ByVal sType As String) As EOSBaseComponent

Dim tType As System.Type

Dim _EOSBase As EOSBaseComponent

Try

tType = System.Type.GetType(sType)

_EOSBase = CType(System.Activator.CreateInstance(tType), EOSBaseComponent)

Return _EOSBase

Catch ex As Exception

Console.WriteLine("Error:" & Me.GetType.ToString() & " " & ex.Message)

End Try

End Function

And type keeps coming back NullReferenceException

Ideas?

CJ

<bu********@yahoo.com> wrote in message
news:01****************************@phx.gbl...
Well, I've done something like that in a differnt way.

I get all Assembly, Class and method on the fly and
execute it.
NOTE: to use "LoadWithPartialName" you need u'r assembly
in the GAC.

'Load the DLL
objRef =
System.Reflection.Assembly.LoadWithPartialName(str DLLName)
If objRef Is Nothing Then Throw New
Exception("Assembly : '" & strDLLName & "' could not be
loaded.")
'Instansiate the Class
objInstance = objRef.CreateInstance
(strClassName)
If objInstance Is Nothing Then Throw New
Exception("Class : '" & strClassName & "' could not be
instantiated.")
'Call the Function
CallByName(objInstance, strMethodName,
CallType.Method, Object)
-----Original Message-----
Nevermind found it.

For thsoe that want the answer, its in the Activator

class. (System
namespace).

I'll let you know how it goes.

-CJ
"CJ Taylor" <ct*****@mortonwelding.com> wrote in message
news:u8**************@TK2MSFTNGP09.phx.gbl...
I think I've seen this somewhere, but I cannot remember how to initialze a class by a string.

That is, I have a class(Component) that I want to

initialize on the fly.
It
is derived from a base component I created.

So basically I have.

Public class myDerivedComponent
inherits myBaseComponent

... stuff ...

End Class

and in another code I want to have

dIm myString as String = "myDerivedComponent"

Dim myD as myBaseComponent

myD.InstanceOf (myString)

Hopefully this makes sense.

-CJ

.

Nov 19 '05 #4
Hi CJ,

There's a post in the languages.csharp newsgroup, "opening form by
name", which uses Reflection to invoke the constructor of the class from
which you want an instance. It allows you to pass parameters, if necessary.

Regards,
Fergus
Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
3
by: Rob F | last post by:
I am trying to compile a string class which was working before but subsequent attempts to make it more 'object orientated' (privatizing the member variables and implenting query functions) it...
3
by: Vincent Cantin | last post by:
I have a class defined by a template which needs to "say" its type to the user via string. As an example, here is the class that I want to fix : template<class T> class Container : public...
4
by: Carl Youngblood | last post by:
I imagine this subject has probably been brought up numerous times. Forgive me for bringing it up again. I was googling through old posts on this newsgroup about it and found a good suggestion on...
18
by: Erik Arner | last post by:
Hi, I really need some help here. After upgrading to g++ 3.4 I have run into all sorts of troubles that I'm sure depends on my lack of proper understanding of C++. I would now like to get it right...
27
by: djake | last post by:
In the stroustrup C++ programming language (third edition) i can find example like this: string s1= "Hello"; So I imagine string is a standard class. Furthermore the class in the example is...
5
by: Tappy Tibbons | last post by:
I have a class I am serializing, and need the resultant XML to skip/omit classes that are not initialized, or their member variables have not been set. Is this possible? Say for the following...
0
by: keith bannister via .NET 247 | last post by:
(Type your message here) -------------------------------- From: keith bannister Hi, I'm new to .net (as of last week) but here goes. I want to serialize/deserialize a file the conforms...
5
by: Keith Bannister | last post by:
I'm new to .net so here goes. I'm tying to deserialize a class that is associated with an XML schema. I created the C# class with xsd.exe as below: xsd.exe /c /n:somenamespace...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.