473,626 Members | 3,365 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1571
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(typeToU se).GetConstruc tor(new
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.co m
"hung tran" <om*****@gmx.ne t> wrote in message
news:ki******** *************** *********@4ax.c om...
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.co m

"Dan Manges" <da***********@ gmail.com> wrote in message
news:jk******** **********@torn ado.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(typeToU se).GetConstruc tor(new
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(typeToUs e)
{
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(typeToU se).GetConstruc tor(new
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**********@g mail.com"
<ja**********@g mail.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(typeToUs e)
{
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.ne t> wrote:
On 6 Jun 2006 08:44:15 -0700, "ja**********@g mail.com"
<ja**********@g mail.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(typeToUs e)
{
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
2570
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 comp.lang.c. :-D When I redid my code into C++ recently, I saw some amazing bogosities. Such as member functions being called before the constructors ran, and member functions being called for NULL objects. Enough to make me switch back to C, where...
16
3982
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
1175
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. I set the "Use Managed Extensions" option to Yes in Configuration Properties General, added the #using <mscorlib.dll> line to the StdAfx.h and attempted to compile. I received the following error: /RTC1 and /clr command-line options are...
4
2060
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 = True, I would then have the Control.BeginInvoke(New AsyncCallback(AddressOf GetDataCallback), New Object() {ar}). How would one achieve the same thing on an asp.net page (without using a webseervice)? In the code below, because the...
2
1500
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 also maintains state for the form variables. The form unload / closing event destroys the business object. For a small set of specialized forms in .NET, I want to try switching the usual order of things. The business class will optionally...
1
1928
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 are those generated by the Server Explorer. I can connect just fine using the default values. But when I comment out strDefaultConnectionFile and uncomment the dbName variable I get: 'OleDbException: Could not use "; file already in use.' ...
669
25833
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 paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990. http://www.ccs.neu.edu/home/cobbe/pl-seminar-jr/notes/2003-sep-26/expressive-slides.pdf
2
2554
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 uses an image map and hrefs. The second layer is where the page will appear. So when a user clicks a link on the palette image, a new 'window' will appear over the palette to show the content. The image of the palette will be slightly faded out to...
1
1787
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 designer') is very slow. How slow? It can take more than 10 seconds for a truely moderate-size form.
0
8265
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, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8196
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
8637
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...
0
8504
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
7193
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 projectplanning, coding, testing, and deploymentwithout 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...
0
4092
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4197
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2625
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 we have to send another system
2
1511
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.