473,326 Members | 2,148 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,326 software developers and data experts.

Switching type at runtime ?

Hi

I use the class Grouper (it's an extended WinForm GroupBox ) for all my
groupboxes, but now the customer wants to be able to switch between Grouper
and GroupBox at runtime, how can I do that ?

My idea is making my own class like MyGroupBox and in the designer I will
call something like this.groupBox2 = new MyGroupBox ();, the class
MyGroupBox will then inherites / adopt either Grouper or GroupBox type at
runtime, but how ?

The attributes of Grouper or GroupBox are almost the same so I don't have
any problems at design time.

Thanks
Jun 6 '06 #1
7 1562
hung tran wrote:
I use the class Grouper (it's an extended WinForm GroupBox ) for all my
groupboxes, but now the customer wants to be able to switch between Grouper
and GroupBox at runtime, how can I do that ?


If you want to decide to use Grouper or GroupBox at run time, you'll
have to use reflection:

string typeToUse = "Grouper";
GroupBox groupBox = (GroupBox)Type.GetType(typeToUse).GetConstructor(n ew
Type[0]).Invoke(new object[0]);

The typecast should work since Grouper is a GroupBox.

Hope this helps.

Dan Manges
Jun 6 '06 #2
hung tran,

You can't choose which type to inherit at runtime.

What you can do is define a common interface which both classes
implement (for the GroupBox, just extend it into another class, and
implement the interface, leaving the rest of the implementation to the
original GroupBox) and then select which to use based on some sort of
configuration.

I have to ask though, what is it that your groupbox does that a regular
groupbox doesn't, and why does the customer want you to switch it?

I would imagine that instead of using a different type, you would just
add a property to your group box which turns your features off.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"hung tran" <om*****@gmx.net> wrote in message
news:ki********************************@4ax.com...
Hi

I use the class Grouper (it's an extended WinForm GroupBox ) for all my
groupboxes, but now the customer wants to be able to switch between
Grouper
and GroupBox at runtime, how can I do that ?

My idea is making my own class like MyGroupBox and in the designer I will
call something like this.groupBox2 = new MyGroupBox ();, the class
MyGroupBox will then inherites / adopt either Grouper or GroupBox type at
runtime, but how ?

The attributes of Grouper or GroupBox are almost the same so I don't have
any problems at design time.

Thanks

Jun 6 '06 #3
Dan,

That's not a good suggestion. The OP's GroupBox might have methods that
he needs to call (beyond what GroupBox offers), as well as a constructor
that is not parameterless.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dan Manges" <da***********@gmail.com> wrote in message
news:jk******************@tornado.ohiordc.rr.com.. .
hung tran wrote:
I use the class Grouper (it's an extended WinForm GroupBox ) for all my
groupboxes, but now the customer wants to be able to switch between
Grouper
and GroupBox at runtime, how can I do that ?


If you want to decide to use Grouper or GroupBox at run time, you'll have
to use reflection:

string typeToUse = "Grouper";
GroupBox groupBox = (GroupBox)Type.GetType(typeToUse).GetConstructor(n ew
Type[0]).Invoke(new object[0]);

The typecast should work since Grouper is a GroupBox.

Hope this helps.

Dan Manges

Jun 6 '06 #4
Nicholas Paldino [.NET/C# MVP] wrote:
Dan,

That's not a good suggestion. The OP's GroupBox might have methods that
he needs to call (beyond what GroupBox offers), as well as a constructor
that is not parameterless.


That's a good point. I was assuming the only methods which were going
to be used were GroupBox methods; otherwise, switching between Grouper
and GroupBox at runtime would be complicated. Thanks for the feedback.

Dan Manges
Jun 6 '06 #5
Well, assuming we are talking about the Grouper descripbed here
(http://www.codeguru.com/csharp/cshar...e.php/c11389/),
it's derived from UserControl, not GroupBox. So the cast would fail.

Also, there's not need to go to Reflection. Everything here is
well-known:

Control groupBox1 = null;
string typeToUse = "Grouper";
switch(typeToUse)
{
case 'Grouper':
groupBox1 = new Grouper();
break;
case 'GroupBox":
groupBox1 = new GroupBox();
break;
}

Dan Manges wrote:
hung tran wrote:
I use the class Grouper (it's an extended WinForm GroupBox ) for all my
groupboxes, but now the customer wants to be able to switch between Grouper
and GroupBox at runtime, how can I do that ?


If you want to decide to use Grouper or GroupBox at run time, you'll
have to use reflection:

string typeToUse = "Grouper";
GroupBox groupBox = (GroupBox)Type.GetType(typeToUse).GetConstructor(n ew
Type[0]).Invoke(new object[0]);

The typecast should work since Grouper is a GroupBox.

Hope this helps.

Dan Manges


Jun 6 '06 #6
On 6 Jun 2006 08:44:15 -0700, "ja**********@gmail.com"
<ja**********@gmail.com> wrote:
Well, assuming we are talking about the Grouper descripbed here
(http://www.codeguru.com/csharp/cshar...e.php/c11389/),

Yes, it is ....
it's derived from UserControl, not GroupBox. So the cast would fail.

Also, there's not need to go to Reflection. Everything here is
well-known:

Control groupBox1 = null;
string typeToUse = "Grouper";
switch(typeToUse)
{
case 'Grouper':
groupBox1 = new Grouper();
break;
case 'GroupBox":
groupBox1 = new GroupBox();
break;
}

Ok, in my Form*.Designer.cs code I have for example:

this.groupBox8 = new MyGroupBox();
this.groupBox9 = new MyGroupBox();

MyGroupBox class is now derived from GroupBox, I will add the missing
properties of Grouper like RoundCorners, ShadowColor etc to it so the code
in Designer.cs will work, but how can I switch to Grouper ?

Thanks
Jun 6 '06 #7
On Tue, 06 Jun 2006 18:30:03 +0200, Omega <om*****@gmx.net> wrote:
On 6 Jun 2006 08:44:15 -0700, "ja**********@gmail.com"
<ja**********@gmail.com> wrote:
Well, assuming we are talking about the Grouper descripbed here
(http://www.codeguru.com/csharp/cshar...e.php/c11389/),

Yes, it is ....
it's derived from UserControl, not GroupBox. So the cast would fail.

Also, there's not need to go to Reflection. Everything here is
well-known:

Control groupBox1 = null;
string typeToUse = "Grouper";
switch(typeToUse)
{
case 'Grouper':
groupBox1 = new Grouper();
break;
case 'GroupBox":
groupBox1 = new GroupBox();
break;
}

Ok, in my Form*.Designer.cs code I have for example:

this.groupBox8 = new MyGroupBox();
this.groupBox9 = new MyGroupBox();

MyGroupBox class is now derived from GroupBox, I will add the missing
properties of Grouper like RoundCorners, ShadowColor etc to it so the code
in Designer.cs will work, but how can I switch to Grouper ?

Thanks


Ops, sorry, used the wrong profile to post -:)

Hung Tran
Jun 6 '06 #8

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

Similar topics

32
by: Kamilche | last post by:
I'm looking at other languages. Some of them fool me into thinking they're useful, and I change my home page to them... 'comp.lang.c++' for instance. But I always end up switching it back to...
16
by: agay | last post by:
Hi, I would like to get feedback on a "switching on strings" utility: http://shum.huji.ac.il/~agay/sos Thanks a. agay
4
by: Tim Hitchcock | last post by:
I have an existing MFC program consisting of an EXE and one DLL that I am trying to convert into a mixed mode program. I am using Visual Studio.NET 2003 Professional and I am compiling in the IDE....
4
by: Jeremy Holt | last post by:
Hi, In a windows.forms application I would BeginInvoke a delegate on the UI thread to collect data from a database. When the call returns to the AsyncCallback, if the Control.InvokeRequired =...
2
by: AllenL | last post by:
Ever since I've been using objects with VB I've instantiated the business object from the form; the business object then creates/destroys data access layer objects as needed. The business object...
1
by: TeeCo | last post by:
Hi folks. I'm trying to change the location of the Access mdb file I connect to using OleDb and am having trouble. I'm using Visual C# 2005 and the default values I use for the ConnectionString...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
2
by: JS | last post by:
Hi all, Im having some troubles with layers on a site im developing. I have two layers one has an image of an artists palette on it and each blob of paint is a link to a page on the site. That...
1
by: SamSpide | last post by:
Hi all, I have a moderate-side 'Windows Form Application' (C++) project, with several forms. For some reason switching between code & designer views (right-click ;view code' or 'view...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.