473,568 Members | 3,014 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 myDerivedCompon ent
inherits myBaseComponent

.... stuff ...

End Class

and in another code I want to have

dIm myString as String = "myDerivedCompo nent"

Dim myD as myBaseComponent

myD.InstanceOf (myString)

Hopefully this makes sense.

-CJ
Nov 19 '05 #1
4 1194
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*****@morton welding.com> wrote in message
news:u8******** ******@TK2MSFTN GP09.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 myDerivedCompon ent
inherits myBaseComponent

... stuff ...

End Class

and in another code I want to have

dIm myString as String = "myDerivedCompo nent"

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********@yah oo.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 "LoadWithPartia lName" you need u'r assembly
in the GAC.

'Load the DLL
objRef =
System.Reflecti on.Assembly.Loa dWithPartialNam e(strDLLName)
If objRef Is Nothing Then Throw New
Exception("Asse mbly : '" & strDLLName & "' could not be
loaded.")
'Instansiate the Class
objInstance = objRef.CreateIn stance
(strClassName)
If objInstance Is Nothing Then Throw New
Exception("Clas s : '" & strClassName & "' could not be
instantiated.")
'Call the Function
CallByName(objI nstance, 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*****@morton welding.com> wrote in message
news:u8******* *******@TK2MSFT NGP09.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 myDerivedCompon ent
inherits myBaseComponent

... stuff ...

End Class

and in another code I want to have

dIm myString as String = "myDerivedCompo nent"

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(B yVal sType As String) As EOSBaseComponen t

Dim tType As System.Type

Dim _EOSBase As EOSBaseComponen t

Try

tType = System.Type.Get Type(sType)

_EOSBase = CType(System.Ac tivator.CreateI nstance(tType), EOSBaseComponen t)

Return _EOSBase

Catch ex As Exception

Console.WriteLi ne("Error:" & Me.GetType.ToSt ring() & " " & ex.Message)

End Try

End Function

And type keeps coming back NullReferenceEx ception

Ideas?

CJ

<bu********@yah oo.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 "LoadWithPartia lName" you need u'r assembly
in the GAC.

'Load the DLL
objRef =
System.Reflecti on.Assembly.Loa dWithPartialNam e(strDLLName)
If objRef Is Nothing Then Throw New
Exception("Asse mbly : '" & strDLLName & "' could not be
loaded.")
'Instansiate the Class
objInstance = objRef.CreateIn stance
(strClassName)
If objInstance Is Nothing Then Throw New
Exception("Clas s : '" & strClassName & "' could not be
instantiated.")
'Call the Function
CallByName(objI nstance, 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*****@morton welding.com> wrote in message
news:u8******* *******@TK2MSFT NGP09.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 myDerivedCompon ent
inherits myBaseComponent

... stuff ...

End Class

and in another code I want to have

dIm myString as String = "myDerivedCompo nent"

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.cshar p 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
3327
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 to turn a previously made String class that deals with char's into a templated String class that uses the template parameter C instead of char. I...
3
2188
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 refuses to compile in Visual C++. Here is the code: http://www.rbf.org.uk/xstring.h When compiling I get the following: \xstring.h(87) : error...
3
2561
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 Serializable { public: T value;
4
9079
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 how to add functionality to the string class. Rather than trying to inherit from the string class, the author was suggesting wrapping the string...
18
5740
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 once and for all, if possible. Most severe is the problem illustrated by the code below. It's based on the "pluggable factory pattern" described...
27
2410
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 initialized with a constructor like this
5
2307
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 code, sometimes the Person may not have given us one, both, or either of their addresses. The serialize function will still put in a...
0
3016
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 to an XML schema (xsd).
5
7273
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 properties.xsd this creates properties.cs
0
7693
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6277
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5501
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.