473,503 Members | 10,322 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1016
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
1501
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
3859
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
9166
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
3350
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
7207
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,...
0
7095
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...
0
7361
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...
0
7470
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...
0
5602
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,...
1
5026
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...
0
4693
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1523
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.