473,657 Members | 2,484 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 = "SomeExistingCl ass"
ClassTypeInStri ng(s) MyThing = new ClassTypeInStri ng(s) ;

....or....

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

I suspect there is something in System.Reflecti on. 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 sSomeStringVari able

Many thanks to the hundreds who reply...

--
Paul
Jan 5 '06 #1
4 1022
I suspect there is something in System.Reflecti on. 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.

"Paulustrio us" <msdn_whoisat_p aulcotter.com> wrote in message
news:58******** *************** ***********@mic rosoft.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 = "SomeExistingCl ass"
ClassTypeInStri ng(s) MyThing = new ClassTypeInStri ng(s) ;

...or....

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

I suspect there is something in System.Reflecti on. 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 sSomeStringVari able

Many thanks to the hundreds who reply...

--
Paul

Jan 5 '06 #2
"Paulustrio us" <msdn_whoisat_p aulcotter.com> a écrit dans le message de
news: 58************* *************** **...icrosof 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 = "SomeExistingCl ass"
| ClassTypeInStri ng(s) MyThing = new ClassTypeInStri ng(s) ;
|
| ...or....
|
| SomeClass myVar = new SomeClass() // and later on we do...
| ClassTypeOfObje ct(myVar) MyThing = new ClassTypeOfObje ct(myVar)
|
| I suspect there is something in System.Reflecti on. 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 sSomeStringVari able

You certainly can use Activator.Creat eInstance(...) 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.Creat eInstance
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,
"Paulustrio us" <msdn_whoisat_p aulcotter.com> wrote in message
news:58******** *************** ***********@mic rosoft.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 = "SomeExistingCl ass"
ClassTypeInStri ng(s) MyThing = new ClassTypeInStri ng(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.vi sible = 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='sa les_ledger'
and user = ?

and in psuedo code we have
open ( new window(string_w indow_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
1505
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 several types of photos, and you find > yourself saying > > if is_mugshot:
96
3949
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 http://www.securitytracker.com/alerts/2004/Feb/1009067.html). All for one measly extra bit. So has the time come for C++ to deprecate unsigned integral types? john
4
9176
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
3369
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 the usual stuff of recreating the usercontrol in the Page Init event. The 'failure' sequence is as follows: - select web form button to display the user control - select user control button, event fires - select web form button to display...
0
8421
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8325
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8844
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8742
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8518
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8621
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7354
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6177
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
1734
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.