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

Create new Form of the same Type as an existing Instance

Hello,

I have an instance of a form, lets say formA.
This form is passed to a method that must create a new form, lets say
formB of the same type.
How can this be accomplished except by using reflection eg;
Type FormType = (formA.GetType());
Form formB = (Form)
Activator.CreateInstance(FormType);

Thank you.

Sep 20 '06 #1
5 1903
Well, if you own the forms, you could always define a factory interface -
similar to ICloneable, but returning a vanilla one of the same? Not ideal
though (it would need constant override methods to work with inheritance,
and doesn't work for existing Form (w/o the interface) instances).

Perhaps reflection is the lesser evil in this scenario, and (lets face it)
in WinForms there's more reflection going on than you would believe, so a
little more isn't going to hurt. And it is a fairly innocent use of
reflection anyway (you aren't hacking around private fields, etc)

Marc
Sep 20 '06 #2
Hi,

in your scneario I would recommend to use a factory method. In order to
implement one, you have to write an interface with a single
CreateInstance Method. Let FormA implement that interface (which
schould return an instance of itself).

Cheers
Gerhard

PS: Creating an instance with reflection cost time and is more hard to
implement and to maintain.

Always have a look at: http://jachman.wordpress.com

tarnap schrieb:
Hello,

I have an instance of a form, lets say formA.
This form is passed to a method that must create a new form, lets say
formB of the same type.
How can this be accomplished except by using reflection eg;
Type FormType = (formA.GetType());
Form formB = (Form)
Activator.CreateInstance(FormType);

Thank you.
Sep 21 '06 #3
Creating an instance with reflection cost time and
is more hard to implement and to maintain.
Harder to implement and maintain?

With the OP's suggested Activator.CreateInstance approach, he needs to do,
erm, nothing. Except keep a default ctor, which most Form types are likely
to have anyway... with the factory approach you have an additional (albeit
trivial) interface, which you need to keep implementing. And every time you
subclass you need to override the implementation, otherwise if "class A :
IFactory", and "class B : A", then "B : IFactory" automatically, but unless
you *always* remember to override, calling b.CreatInstance() will return a
new A, not a new B. It also can't be used on Form types that you don't own.

Generics /might/ give a workable solution in this scenario via the "where T
: new()" clause, but I doubt the type is fixed at runtime (although maybe a
little redesign using generics and interfaces might yield a successful
conclusion).

Finally, in forms performance, I don't think anybody is going to really
notice the tiny performance hit for using reflection. You don't generally
create forms in a tight loop, and there is enough going on anyway (PInvoke
to get OS handles and set Win32 flags, reflection internals inside WinForms
itself, etc) that it is *absolutely* a premature optimisation to worry about
this.

So: the premature optimisation *introduces* a maintenance overhead
(subclassing etc), and a potential cause of bugs (forgetting to override
etc), and can't be applied as widely... personally I'd stick with reflection
here.

Marc

Sep 21 '06 #4
where T : new()

Should have been

where T : Form, new()

Marc
Sep 21 '06 #5
I will be using reflection on this one, for ease of implementation.
Thank you!

Sep 21 '06 #6

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

Similar topics

6
by: tzach | last post by:
Call constructor on an already existing instance? I like to execute the constructor logic, including all the class member constructors, on an existing instance (with out executing the destructor)....
1
by: Paul Bromley | last post by:
Please help with this one - I have scoured the newsgroups for information on this, and all of the responses relate to how to refernce controls on a NEW instanc of a form and not an exitsing one. I...
15
by: Cruella DeVille | last post by:
I'm trying to implement a bookmark-url program, which accepts user input and puts the strings in a dictionary. Somehow I'm not able to iterate myDictionary of type Dict{} When I write print...
7
by: Roemer | last post by:
Hi all I stumbled over a new problem: I have a programm with just a class that is asynchronous listening for network connections. As soon as someone connected, a new form needs to be created....
18
by: Sandra-24 | last post by:
Can you create an instance of a subclass using an existing instance of the base class? Such things would be impossible in some languages or very difficult in others. I wonder if this can be done...
1
by: Allen | last post by:
I need a way to add a method to an existing instance, but be as close as possible to normal instance methods. Using 'new' module or such code as 'def addfunc(...): def helper(...) .. setattr(...)'...
6
by: Peter Morris | last post by:
My app is a single-instance one. When a second instance is executed I need to pass the startup arguments to the existing instance. Can anyone recommend a good approach for this? I tried the...
0
by: Neville Dempsey | last post by:
Basically I have an existing (maybe a rather large and complicated (existing) instance) that I want to add new member to. Cheers N Hacks/attempts follow: from math import sqrt
0
by: Neville Dempsey | last post by:
What do I need to add to HTMLDecorator? A simpler example: import cgi class ClassX(object): pass # ... with own __repr__ class ClassY(object):
1
by: =?ISO-8859-1?Q?Andr=E9?= | last post by:
Hi, I was trying to find a way to set, upon __init__() the parent of a class to an existing instance. Here is a minimal example of what I'm trying to do: class A(object): def __init__(self, x):...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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,...
0
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
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...
0
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
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,...

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.