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

C# generic constructor for contol type

427 Expert 256MB
Hi,

i am trying to create a generic class (C# .NET 2.0) which accepts any control,
i am having problems creating a constructor which allows me to access the value supplied as the generic type:

Expand|Select|Wrap|Line Numbers
  1. public class DynamicControl<C> where C:Control
thank you
Jun 23 '08 #1
4 1777
Plater
7,872 Expert 4TB
Why do you need this?
Anything that takes "Control" as a parameter will ALREADY take anything that inherits from control (Textbox, Label, Button, etc etc etc)
Jun 23 '08 #2
cloud255
427 Expert 256MB
I would to create a class which makes instances of controls at runtime,
further the user must be able to pick which control to add to the form.

I already have it working for adding , moving and resizing a control at runtime, the only problem is that i have to specify the type of control in code:

Expand|Select|Wrap|Line Numbers
  1. Control cntrl = new Button
for example.

i would like a class that will just accept a Control and apply the general methods i need to that control.

C# complains when i try to say:

Expand|Select|Wrap|Line Numbers
  1. Class DynmaicControl<C> where C:Control
  2. {
  3.   DynamicControl(Several Arguments)
  4.   {
  5.          C someVariable = new C(); // this causes a problem
  6.   }
  7. }
thanks
Jun 24 '08 #3
Plater
7,872 Expert 4TB
I think you need something inside the System.Reflection namesapce. I hear people use that to instansiate classes
Jun 24 '08 #4
cloud255
427 Expert 256MB
I think you need something inside the System.Reflection namesapce. I hear people use that to instansiate classes
Thanks for the advice, it pointed me in the right direction.

by the way for anyone struggling with a similiar issue:

Original attempt:
Expand|Select|Wrap|Line Numbers
  1. Class DynamicControl<C> Where C:Control
Solution:
Expand|Select|Wrap|Line Numbers
  1. Class DynamicControl<C> Where C:Control, new()
this allows you to call the deafult contsructor for the given type by simply saying: C cntrl = new C();

as far as i can tell adding new() in the class inheritence allows C# to invoke the deafult constructor for any .NET base type.
Jun 25 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

49
by: Steven Bethard | last post by:
I promised I'd put together a PEP for a 'generic object' data type for Python 2.5 that allows one to replace __getitem__ style access with dotted-attribute style access (without declaring another...
9
by: Alon Fliess | last post by:
Hi I am trying to write a generic class that instantiates the generic type, but I can not find the correct way to give it the constructor constraint. For example: In C#: class X<T> where...
7
by: Andrus | last post by:
public class BusinessObjectGeneric<EntityType: BusinessObject where EntityType : BusinessEntity, new() { public BusinessObjectGeneric<EntityType() { } ..... causes error in constructor...
7
by: WT | last post by:
Hello, I am building a generic class that needs to instanciate an object of its template class, something like public class MyGeneric<T> { List<TmyList = new List<T>(); void addToLst(List...
2
by: gilbert | last post by:
Hello. I am trying to use c# generic to define a class. class MyClass<Twhere T: new(){ } In this definition, MyClass can create T objects with a default constructor. Is there any way to...
10
by: fig000 | last post by:
HI, I'm new to generics. I've written a simple class to which I'm passing a generic list. I'm able to pass the list and even pass the type of the list so I can use it to traverse it. It's a...
9
by: Andrus | last post by:
I need to create instance of generic type when generic type does not have parameterless constructor. I tried code below but got error shown in comment. How to fix ? Andrus. abstract class...
9
by: tadmill | last post by:
Is it possible to pass a generic parameter of the same class to to its constructor, where the "T" type passed in the constructor is different than the "T" type of the instanced class? ie, ...
0
by: SimonDotException | last post by:
I've written an abstract base type which uses generics to provide XML serialization and deserialization methods for use by its derived types, but I'm seemingly unable to write it in a way which...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.