Creating objects at runtime

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kimmo Laine

    #1

    Creating objects at runtime

    Hello,

    (how) can i do the following (in C#)?

    // . . .
    class MyForm : Form {
    // . . .

    protected Button myButton = null;
    protected TextBox myTB = null;

    protected void AddControl( ref Control ctrl, string strType, Size s,
    Point pt, string txt ) {

    ctrl = new < ?!?! >;
    ctrl.Location = pt;
    ctrl.Size = s;
    ctrl.Text = txt;

    Controls.Add( ctrl );

    }

    protected override void OnLoad( EventArgs e ) {

    AddControl( myButton, "Button", new Size( ...), new Point( ... ),
    "Dummy 1" );
    AddControl( myTB, "TextBox", new Size( ...), new Point( ... ),
    "Dummy 1" );

    base.OnLoad( e );
    }
    }

    It would be even better if the second parameter ( in AddControl ) could be
    the actual type:

    AddControl( myButton, Button, new Size( ...), new Point( ... ),
    "Dummy 1" );
    AddControl( myTB, TextBox, new Size( ...), new Point( ... ), "Dummy
    1" );


    Thx!


    - Kimmo Laine


  • Dmitriy Lapshin [C# / .NET MVP]

    #2
    Re: Creating objects at runtime

    Hi Kimmo,

    You can use the following classes and methods:

    Assembly.Create Instance(...) - this one accepts full type name
    Activator.Creat eInstance(...) - this one accepts a Type instance


    --
    Dmitriy Lapshin [C# / .NET MVP]
    X-Unity Test Studio

    Bring the power of unit testing to VS .NET IDE

    "Kimmo Laine" <reply.to@newsg roup> wrote in message
    news:OnpeTQYjDH A.3700@TK2MSFTN GP11.phx.gbl...[color=blue]
    > Hello,
    >
    > (how) can i do the following (in C#)?
    >
    > // . . .
    > class MyForm : Form {
    > // . . .
    >
    > protected Button myButton = null;
    > protected TextBox myTB = null;
    >
    > protected void AddControl( ref Control ctrl, string strType, Size s,
    > Point pt, string txt ) {
    >
    > ctrl = new < ?!?! >;
    > ctrl.Location = pt;
    > ctrl.Size = s;
    > ctrl.Text = txt;
    >
    > Controls.Add( ctrl );
    >
    > }
    >
    > protected override void OnLoad( EventArgs e ) {
    >
    > AddControl( myButton, "Button", new Size( ...), new Point( ... ),
    > "Dummy 1" );
    > AddControl( myTB, "TextBox", new Size( ...), new Point( ... ),
    > "Dummy 1" );
    >
    > base.OnLoad( e );
    > }
    > }
    >
    > It would be even better if the second parameter ( in AddControl ) could be
    > the actual type:
    >
    > AddControl( myButton, Button, new Size( ...), new Point( ... ),
    > "Dummy 1" );
    > AddControl( myTB, TextBox, new Size( ...), new Point( ... ),[/color]
    "Dummy[color=blue]
    > 1" );
    >
    >
    > Thx!
    >
    >
    > - Kimmo Laine
    >
    >[/color]

    Comment

    • Kimmo Laine

      #3
      Re: Creating objects at runtime

      Hi,

      don?t work

      Type t = Type.GetType( "System.Windows .Forms.Button" );
      Button b = (Button)Activat or.CreateInstan ce( t );

      - Kimmo Laine


      "Dmitriy Lapshin [C# / .NET MVP]" <x-code@no-spam-please.hotpop.c om> wrote
      in message news:#97ScpYjDH A.2216@TK2MSFTN GP12.phx.gbl...[color=blue]
      > Hi Kimmo,
      >
      > You can use the following classes and methods:
      >
      > Assembly.Create Instance(...) - this one accepts full type name
      > Activator.Creat eInstance(...) - this one accepts a Type instance
      >
      >
      > --
      > Dmitriy Lapshin [C# / .NET MVP]
      > X-Unity Test Studio
      > http://x-unity.miik.com.ua/teststudio.aspx
      > Bring the power of unit testing to VS .NET IDE
      >
      > "Kimmo Laine" <reply.to@newsg roup> wrote in message
      > news:OnpeTQYjDH A.3700@TK2MSFTN GP11.phx.gbl...[color=green]
      > > Hello,
      > >
      > > (how) can i do the following (in C#)?
      > >
      > > // . . .
      > > class MyForm : Form {
      > > // . . .
      > >
      > > protected Button myButton = null;
      > > protected TextBox myTB = null;
      > >
      > > protected void AddControl( ref Control ctrl, string strType, Size s,
      > > Point pt, string txt ) {
      > >
      > > ctrl = new < ?!?! >;
      > > ctrl.Location = pt;
      > > ctrl.Size = s;
      > > ctrl.Text = txt;
      > >
      > > Controls.Add( ctrl );
      > >
      > > }
      > >
      > > protected override void OnLoad( EventArgs e ) {
      > >
      > > AddControl( myButton, "Button", new Size( ...), new Point([/color][/color]
      .... ),[color=blue][color=green]
      > > "Dummy 1" );
      > > AddControl( myTB, "TextBox", new Size( ...), new Point( ... ),
      > > "Dummy 1" );
      > >
      > > base.OnLoad( e );
      > > }
      > > }
      > >
      > > It would be even better if the second parameter ( in AddControl ) could[/color][/color]
      be[color=blue][color=green]
      > > the actual type:
      > >
      > > AddControl( myButton, Button, new Size( ...), new Point( ... ),
      > > "Dummy 1" );
      > > AddControl( myTB, TextBox, new Size( ...), new Point( ... ),[/color]
      > "Dummy[color=green]
      > > 1" );
      > >
      > >
      > > Thx!
      > >
      > >
      > > - Kimmo Laine
      > >
      > >[/color]
      >[/color]


      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: Creating objects at runtime

        Kimmo,

        This doesn't work because you are making the call to GetType outside of
        the assembly that the type is in. In the documentation for the GetType
        method on the Type class, it will tell you how to get the fully-qualified
        type name, which is what you need to use for types contained in assemblies
        outside of the one where the call to GetType is made.

        Hope this helps.


        --
        - Nicholas Paldino [.NET/C# MVP]
        - nick(dot)paldin o=at=exisconsul ting<dot>com

        "Kimmo Laine" <reply.to@newsg roup> wrote in message
        news:OEEp4kZjDH A.2512@TK2MSFTN GP09.phx.gbl...[color=blue]
        > Hi,
        >
        > don?t work
        >
        > Type t = Type.GetType( "System.Windows .Forms.Button" );
        > Button b = (Button)Activat or.CreateInstan ce( t );
        >
        > - Kimmo Laine
        >
        >
        > "Dmitriy Lapshin [C# / .NET MVP]" <x-code@no-spam-please.hotpop.c om> wrote
        > in message news:#97ScpYjDH A.2216@TK2MSFTN GP12.phx.gbl...[color=green]
        > > Hi Kimmo,
        > >
        > > You can use the following classes and methods:
        > >
        > > Assembly.Create Instance(...) - this one accepts full type name
        > > Activator.Creat eInstance(...) - this one accepts a Type instance
        > >
        > >
        > > --
        > > Dmitriy Lapshin [C# / .NET MVP]
        > > X-Unity Test Studio
        > > http://x-unity.miik.com.ua/teststudio.aspx
        > > Bring the power of unit testing to VS .NET IDE
        > >
        > > "Kimmo Laine" <reply.to@newsg roup> wrote in message
        > > news:OnpeTQYjDH A.3700@TK2MSFTN GP11.phx.gbl...[color=darkred]
        > > > Hello,
        > > >
        > > > (how) can i do the following (in C#)?
        > > >
        > > > // . . .
        > > > class MyForm : Form {
        > > > // . . .
        > > >
        > > > protected Button myButton = null;
        > > > protected TextBox myTB = null;
        > > >
        > > > protected void AddControl( ref Control ctrl, string strType, Size[/color][/color][/color]
        s,[color=blue][color=green][color=darkred]
        > > > Point pt, string txt ) {
        > > >
        > > > ctrl = new < ?!?! >;
        > > > ctrl.Location = pt;
        > > > ctrl.Size = s;
        > > > ctrl.Text = txt;
        > > >
        > > > Controls.Add( ctrl );
        > > >
        > > > }
        > > >
        > > > protected override void OnLoad( EventArgs e ) {
        > > >
        > > > AddControl( myButton, "Button", new Size( ...), new Point([/color][/color]
        > ... ),[color=green][color=darkred]
        > > > "Dummy 1" );
        > > > AddControl( myTB, "TextBox", new Size( ...), new Point( ... ),
        > > > "Dummy 1" );
        > > >
        > > > base.OnLoad( e );
        > > > }
        > > > }
        > > >
        > > > It would be even better if the second parameter ( in AddControl )[/color][/color][/color]
        could[color=blue]
        > be[color=green][color=darkred]
        > > > the actual type:
        > > >
        > > > AddControl( myButton, Button, new Size( ...), new Point([/color][/color][/color]
        .... ),[color=blue][color=green][color=darkred]
        > > > "Dummy 1" );
        > > > AddControl( myTB, TextBox, new Size( ...), new Point( ... ),[/color]
        > > "Dummy[color=darkred]
        > > > 1" );
        > > >
        > > >
        > > > Thx!
        > > >
        > > >
        > > > - Kimmo Laine
        > > >
        > > >[/color]
        > >[/color]
        >
        >[/color]


        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Creating objects at runtime

          Kimmo Laine <reply.to@newsg roup> wrote:[color=blue]
          > Type t = Type.GetType( "System.Windows .Forms.Button" );
          > Button b = (Button)Activat or.CreateInstan ce( t );[/color]

          When Type.GetType is only given the name of the type without
          assembly/version information, it only looks in the currently executing
          assembly and mscorlib. If you want it to look elsewhere, you should
          either provide the assembly/version information, or use
          Assembly.GetTyp e instead on the appropriate assembly.

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          Working...