Switching type at runtime ?

  • Thread starter Thread starter hung tran
  • Start date Start date
H

hung tran

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
 
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.
 
hung said:
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(new
Type[0]).Invoke(new object[0]);

The typecast should work since Grouper is a GroupBox.

Hope this helps.

Dan Manges
 
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]
- (e-mail address removed)

Dan Manges said:
hung said:
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(new
Type[0]).Invoke(new object[0]);

The typecast should work since Grouper is a GroupBox.

Hope this helps.

Dan Manges
 
Nicholas said:
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
 
Well, assuming we are talking about the Grouper descripbed here
(http://www.codeguru.com/csharp/csharp/cs_controls/custom/article.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 said:
hung said:
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(new
Type[0]).Invoke(new object[0]);

The typecast should work since Grouper is a GroupBox.

Hope this helps.

Dan Manges
 
Well, assuming we are talking about the Grouper descripbed here
(http://www.codeguru.com/csharp/csharp/cs_controls/custom/article.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
 
Yes, it is ....



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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top