473,239 Members | 1,493 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,239 software developers and data experts.

Run time decision on type of new object

Is it possible to decide on the type of an object at run time. So in
pseudo-code I am looking for one (or preferably both) of the following....

string s = "SomeExistingClass"
ClassTypeInString(s) MyThing = new ClassTypeInString(s) ;

....or....

SomeClass myVar = new SomeClass() // and later on we do...
ClassTypeOfObject(myVar) MyThing = new ClassTypeOfObject(myVar)

I suspect there is something in System.Reflection. I dipped a toe in there,
but the water looks awfully deep for a beginner. I am trying to convert some
powerbuilder code and I want the equivalent of...

myObj = Create using sSomeStringVariable

Many thanks to the hundreds who reply...

--
Paul
Jan 5 '06 #1
4 1013
I suspect there is something in System.Reflection. I dipped a toe in there, but the water looks awfully deep for a beginner. I am trying to convert some

:) I heard something like "he didnt know it was impossible so he did it!" I
always remember this :)

Have you tried looking at the Activator class. It does something like what u
want to do. It has a CreateInstance method that does that :)

Abubakar.

"Paulustrious" <msdn_whoisat_paulcotter.com> wrote in message
news:58**********************************@microsof t.com... Is it possible to decide on the type of an object at run time. So in
pseudo-code I am looking for one (or preferably both) of the following....

string s = "SomeExistingClass"
ClassTypeInString(s) MyThing = new ClassTypeInString(s) ;

...or....

SomeClass myVar = new SomeClass() // and later on we do...
ClassTypeOfObject(myVar) MyThing = new ClassTypeOfObject(myVar)

I suspect there is something in System.Reflection. I dipped a toe in there, but the water looks awfully deep for a beginner. I am trying to convert some powerbuilder code and I want the equivalent of...

myObj = Create using sSomeStringVariable

Many thanks to the hundreds who reply...

--
Paul

Jan 5 '06 #2
"Paulustrious" <msdn_whoisat_paulcotter.com> a écrit dans le message de
news: 58**********************************@microsoft.com...

| Is it possible to decide on the type of an object at run time. So in
| pseudo-code I am looking for one (or preferably both) of the following....
|
| string s = "SomeExistingClass"
| ClassTypeInString(s) MyThing = new ClassTypeInString(s) ;
|
| ...or....
|
| SomeClass myVar = new SomeClass() // and later on we do...
| ClassTypeOfObject(myVar) MyThing = new ClassTypeOfObject(myVar)
|
| I suspect there is something in System.Reflection. I dipped a toe in
there,
| but the water looks awfully deep for a beginner. I am trying to convert
some
| powerbuilder code and I want the equivalent of...
|
| myObj = Create using sSomeStringVariable

You certainly can use Activator.CreateInstance(...) to do things like this,
it works with either strings or Types.

The practice of using strings to decide runtime behaviour is prone to error
due to mis-spelling, and other things. This style of programming is really a
hangover from the days of interpreters like BASIC, Clipper and others. Why
would you want to use strings to make these decisions ?

OTOH, IMO, you would be better advised to use variables of Type to hold the
type that you want to instantiate.

There is a Design Pattern called Class Factory which is recommended, usually
to create instances of derived classes into an abstract class reference or
an interface reference.

Do you have a hierarchy of classes in mind or just any class that comes
along ?
If the former, then I would recommend that you use Activator.CreateInstance
inside a Class Factory class; if the latter, then I personally would
recommend you redesign the way things are being done.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jan 5 '06 #3
Hi,
"Paulustrious" <msdn_whoisat_paulcotter.com> wrote in message
news:58**********************************@microsof t.com...
Is it possible to decide on the type of an object at run time. So in
pseudo-code I am looking for one (or preferably both) of the following....

string s = "SomeExistingClass"
ClassTypeInString(s) MyThing = new ClassTypeInString(s) ;

Hi, you can use one of the CreateInstance method , depending of how/(where
from) you want to instantiate the class.

But that is only half of the problem, how do you intend to use this new
instance?
Unless that all the possible classes share a common interface you will need
to instantiate it in an object variable, (as you do not know beforehand the
real type of the instance).
You would have to use reflection to use the members of the isntance. No a
very elegant thing.

If they support a common interface then all is easy, the "real" type of the
instance is not important as you do have a concrete interface to interact
with it.

Under this presunction then you may wrap the creation of the classes itself
, in such a way that only one class (the factory) knows how to instantiate
the concrete type you will be using. This is called a Factory pattern:
http://en.wikipedia.org/wiki/Factory_method_pattern

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jan 5 '06 #4
Thank you all for your replies. Sorry this is so late.

An existing application directed what user could do from a database. So for
example there may be a column called EditMenu which for each user defined
which Editmenu was instantiated at run time.

It actually goes deeper than. There was a rich inheritance structure. So
rather than disabling facilities at run time as in...

If (role == "personnel") Button_LegGo.visible = true

There is a window window_Personel inheriting from win_Internal inheriting
from win_Everyone. So before opening a window there is a stored procedure
that does something like...

select window_class, menu_class from window
where window_type='sales_ledger'
and user = ?

and in psuedo code we have
open ( new window(string_window_class) )

I have to say it works well. Database constraints enforce real window names.
You can distribute a new version of an app with version 1 and 2 of a window
and then on an indivdual, departmental or universal basis switch from version
1 to version 2 ... and if it turns into a dog's dinner switch back.

The other big advantage is you can see the window at design time as the user
sees it rather than imagining that various controls and menu items are/not
disabled, resized, invisible, relabeled, different language depending on who
is looking at it.

I don't want to rewrite the complete methodology. So I will look into all
you have said, but I would like to know if you still agree with Class Factory
as the solution.

I understand your points about Basic / Clipper etc. However this is not an
unstructured system. The abstraction of the window name I think can be a
benefit.

After all, when we get down to machine code it's all if-goto.. :)
--
Paul
Jan 11 '06 #5

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

Similar topics

7
by: Chinook | last post by:
OO approach to decision sequence? --------------------------------- In a recent thread (Cause for using objects?), Chris Smith replied with (in part): > If your table of photo data has...
96
by: John Harrison | last post by:
I knew that unsigned integral data types were the cause of scads of mostly spurious warning messages, but I didn't realise that they were a security risk too (see here...
4
by: Saradhi | last post by:
Is there any way to estimate the time required to execute a T-SQL statement? I need to set the command time out by calculating the time for T-SQL statement. Can any one give an example?
6
by: Steve Booth | last post by:
I have a web form with a button and a placeholder, the button adds a user control to the placeholder (and removes any existing controls). The user control contains a single button. I have done all...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
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
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.