Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Fun with generics

Fun with generics

Scheduled Pinned Locked Moved C#
databasedesignregexhelptutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tantiboh
    wrote on last edited by
    #1

    Here's a good problem. I'm trying to set up a system where text fields in a database define dynamically-generated controls on the form. In other words, when a user makes a selection on the form, the system will pull all the records that match that selection from a database that contains text information that defines all the controls from one table, and text information that defines each of those controls' properties from a related table. Now, I could set up switch statements that would satisfy many of the possible permutations. But that would require hundreds of lines of code, and it wouldn't be feasible to predict all the possibilities. My idea is to use generics (which I'm just learning about) to dynamically define the controls and their properties. In essense, I need to somehow get text from the database into the utilization of the generic. Conceptually, something along these lines: **** public class GenericControl { **Code to construct the control** } class TestGeneric { private void BuildControl() { //In reality, this string would be retrieved from the database. string controltype = "System.Web.UI.WebControls.TextBox"; //Then the part that won't work: GenericControl MyControl = new GenericControl(); } } **** Now, if I understand correctly, generic utilization requires that you use a defined object where I have "controltype" - which is why the above example won't work. Is there any way to take the string "System.Web.UI.WebControls.TextBox" and use it to get a TextBox object type (or the appropriate object type) - without using a switch statement?

    G L 2 Replies Last reply
    0
    • T tantiboh

      Here's a good problem. I'm trying to set up a system where text fields in a database define dynamically-generated controls on the form. In other words, when a user makes a selection on the form, the system will pull all the records that match that selection from a database that contains text information that defines all the controls from one table, and text information that defines each of those controls' properties from a related table. Now, I could set up switch statements that would satisfy many of the possible permutations. But that would require hundreds of lines of code, and it wouldn't be feasible to predict all the possibilities. My idea is to use generics (which I'm just learning about) to dynamically define the controls and their properties. In essense, I need to somehow get text from the database into the utilization of the generic. Conceptually, something along these lines: **** public class GenericControl { **Code to construct the control** } class TestGeneric { private void BuildControl() { //In reality, this string would be retrieved from the database. string controltype = "System.Web.UI.WebControls.TextBox"; //Then the part that won't work: GenericControl MyControl = new GenericControl(); } } **** Now, if I understand correctly, generic utilization requires that you use a defined object where I have "controltype" - which is why the above example won't work. Is there any way to take the string "System.Web.UI.WebControls.TextBox" and use it to get a TextBox object type (or the appropriate object type) - without using a switch statement?

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      I think that you will have much better luck using reflection instead. Use the GetConstructors method of the Type class to get an array of ConstructorInfo objects. Use the Invoke method to create the object. --- b { font-weight: normal; }

      1 Reply Last reply
      0
      • T tantiboh

        Here's a good problem. I'm trying to set up a system where text fields in a database define dynamically-generated controls on the form. In other words, when a user makes a selection on the form, the system will pull all the records that match that selection from a database that contains text information that defines all the controls from one table, and text information that defines each of those controls' properties from a related table. Now, I could set up switch statements that would satisfy many of the possible permutations. But that would require hundreds of lines of code, and it wouldn't be feasible to predict all the possibilities. My idea is to use generics (which I'm just learning about) to dynamically define the controls and their properties. In essense, I need to somehow get text from the database into the utilization of the generic. Conceptually, something along these lines: **** public class GenericControl { **Code to construct the control** } class TestGeneric { private void BuildControl() { //In reality, this string would be retrieved from the database. string controltype = "System.Web.UI.WebControls.TextBox"; //Then the part that won't work: GenericControl MyControl = new GenericControl(); } } **** Now, if I understand correctly, generic utilization requires that you use a defined object where I have "controltype" - which is why the above example won't work. Is there any way to take the string "System.Web.UI.WebControls.TextBox" and use it to get a TextBox object type (or the appropriate object type) - without using a switch statement?

        L Offline
        L Offline
        Le centriste
        wrote on last edited by
        #3

        In Generics, types must be known compile-time, not run-time, and do not instance parameters (controltype is an instance of the System.String class). The suggestion from Guffa is very good, but you could still avoid having tons of switches in your code. Start from the fact that all your controls inherit from the System.Windows.Forms.Control class, which, I would say, contains about 95% of the properties of the built-in controls (Size, Font, Text, Position, Name, Visible, etc.). So, using reflection, you could, as suggested by Guffa, instanciate your controls in a way similar to the one below:

        List<Control> controls = new List<Control>();

        foreach (....)
        {
        Type myControlType = Type.ReflectionOnlyGetType(controltype, true, false);
        ConstructorInfo myControlContructor = myControlType.GetConstructor(Type.EmtpyTypes); // Default
        Control myControl = myControlContructor.Invoke(null);

        controls.Add(myControl);
        

        }

        // It is more efficient to declare the variables outside the loop, though.

        Then, set all the properties common to all controls, I think this would cover much of your cases. You will need some mechanism (maybe a flag from your database) to indicate properties that are not defined in the System.Windows.Forms.Control class. Hope this helps, Michel -------- "I say no to drugs, but they don't listen." - Marilyn Manson -- modified at 7:39 Friday 27th January, 2006

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups