Passing Items into a sub

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

    Passing Items into a sub

    Ok, so I have this sub I wrote, and I create a new instance of a
    UserControl:

    ctrlAPs tempctrl = new ctrlAPs();

    Now, I would like to be able to use this sub I wrote for more than one
    UserControl, so I was trying to do something like this:

    private void somesub(UserCon trol sourcectrl)
    {
    sourcectrl tempctrl = new sourcectrl();
    }

    But when I do that, I get the following error on compile:
    "The type or namespace name 'sourcectrl' could not be found (are you
    missing a using directive or an assembly reference?)"

    So, I'm obviously not passing this in right. There has to be a way to
    do this, as I do it with panels and other controls. Any help is
    appreciated.

  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Passing Items into a sub

    Chris,

    You could use Generics, like so:

    private void somesub<T>(T sourcectrl) where T : UserControl, new()
    {
    T tempctrl = new sourcectrl();
    }

    This will allow you to use the type parameter T and pass in any type
    that derives from UserControl as well as has a default, parameterless
    constructor.

    In the method, you will only be able to call methods that exist on the
    UserControl class, nothing that is derived from it.

    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Chris" <coz1978@gmail. comwrote in message
    news:1188309147 .235622.3580@g4 g2000hsf.google groups.com...
    Ok, so I have this sub I wrote, and I create a new instance of a
    UserControl:
    >
    ctrlAPs tempctrl = new ctrlAPs();
    >
    Now, I would like to be able to use this sub I wrote for more than one
    UserControl, so I was trying to do something like this:
    >
    private void somesub(UserCon trol sourcectrl)
    {
    sourcectrl tempctrl = new sourcectrl();
    }
    >
    But when I do that, I get the following error on compile:
    "The type or namespace name 'sourcectrl' could not be found (are you
    missing a using directive or an assembly reference?)"
    >
    So, I'm obviously not passing this in right. There has to be a way to
    do this, as I do it with panels and other controls. Any help is
    appreciated.
    >

    Comment

    • Marc Gravell

      #3
      Re: Passing Items into a sub

      Based on your example, it looks like generics *might* be the answer,
      i.e.

      private T somesub<T>() where T : UserControl, new() {
      T tempctrl = new T();
      // some other code on tempctrl that uses the properties of
      UserControl
      return tempctrl;
      }

      The other option is for the caller to create the control locally,
      before using your code just to initialize it, in which case you just
      need
      private void somesub(UserCon trol control) {
      // configure control using the properties of UserControl
      }

      If not, please clarify what you want to do...

      Marc


      Comment

      • Ignacio Machin \( .NET/ C# MVP \)

        #4
        Re: Passing Items into a sub

        Hi,

        "Chris" <coz1978@gmail. comwrote in message
        news:1188309147 .235622.3580@g4 g2000hsf.google groups.com...
        Ok, so I have this sub I wrote, and I create a new instance of a
        UserControl:
        sub??? You mean method right? :)
        ctrlAPs tempctrl = new ctrlAPs();
        >
        Now, I would like to be able to use this sub I wrote for more than one
        UserControl, so I was trying to do something like this:
        >
        private void somesub(UserCon trol sourcectrl)
        {
        sourcectrl tempctrl = new sourcectrl();
        }
        But when I do that, I get the following error on compile:
        "The type or namespace name 'sourcectrl' could not be found (are you
        missing a using directive or an assembly reference?)"
        >
        So, I'm obviously not passing this in right. There has to be a way to
        do this, as I do it with panels and other controls. Any help is
        appreciated.
        Why are you creating the UserControl inside the method?
        What decide which UserControl to create?
        How you return the new instance to the calling code?

        What if you create the user control outside and pass it to the method?


        Comment

        • Chris

          #5
          Re: Passing Items into a sub

          Yeah, I meant method... grr, still stuck in VB6 mode... Anyways, this
          is the whole sub I'm working on:

          private void test<T>(Panel targetpanel, string sQuery, string
          sQueryParamName , string sParamVal, T sourcectrl) where
          T:UserControl,n ew()
          {
          string Parameter;
          conn = "Provider=Micro soft.Jet.OLEDB. 4.0;Data Source=T:\
          \RFQMT\\RFQMT Test\\RFQMT Data.mdb;Jet OLEDB:Database
          Password=tennis ";
          OleDbConnection oleconn = new OleDbConnection (conn);
          oleconn.Open();

          Parameter = txtMemo.Text.To String();

          OleDbCommand cmd = new OleDbCommand(sQ uery, oleconn);
          cmd.CommandType = CommandType.Sto redProcedure;
          cmd.Parameters. Add(new OleDbParameter( sQueryParamName ,
          sParamVal));
          DataTable dt = new DataTable("APs" );

          OleDbDataAdapte r da = new OleDbDataAdapte r(cmd);
          da.Fill(ds, "AP");

          DataSource = new DataRowCollecti onSocket(ds.AP. Rows);


          for (int i = 0; i < DataSource.Coun t; i++)
          {
          T tempctrl = new UserControl();
          //ctrlAPs tempctrl = new ctrlAPs();
          pnlAP.Controls. Add(tempctrl);
          tempctrl.Top = i * tempctrl.Height - 2;
          tempctrl.Left = 0;

          foreach (Control c in ctrlAPs1.Contro ls)
          {
          Type ctrlType = c.GetType();
          ConstructorInfo cInfo =
          ctrlType.GetCon structor(Type.E mptyTypes);
          Control retControl = (Control)cInfo. Invoke(null);

          foreach (Binding ctrlBinding in c.DataBindings)
          {
          string BindingMember =
          ctrlBinding.Bin dingMemberInfo. BindingMember;
          string BindingField =
          BindingMember.S ubstring(Bindin gMember.LastInd exOf("."));

          tempctrl.Contro ls[c.Name].DataBindings.C lear();

          tempctrl.Contro ls[c.Name].DataBindings.A dd(ctrlBinding. PropertyName,
          DataSource[i], BindingField);
          }
          PropertyInfo DSource =
          ctrlType.GetPro perty("DataSour ce");
          if (DSource != null)
          DSource.SetValu e(retControl,
          DSource.GetValu e(c, null), null);
          }
          ctrlAPs2.Hide() ;
          }
          oleconn.Close() ;
          }

          Originally, I had the query and usercontrol information hard coded.
          It was a 'continuous form' of sorts, like you'd see in an Access
          form. Now I had different usercontrols that acted as different
          'forms', but I didn't see any need in making one whole method per
          control, which is why I'm trying to pass everything into this one.
          Basically it clones the control onto a panel and rebinds the controls
          in that panel to a dataset. Now like I said, I could pass in the
          panel (i.e. which panel on my form I wanted the control copied to),
          but the usercontrol I couldn't. That's why my question is, how to
          pass in the control information like I can pass in the panel. HTH

          On Aug 28, 10:07 am, "Ignacio Machin \( .NET/ C# MVP \)" <machin TA
          laceupsolutions .comwrote:
          Hi,
          >
          "Chris" <coz1...@gmail. comwrote in message
          >
          news:1188309147 .235622.3580@g4 g2000hsf.google groups.com...
          >
          Ok, so I have this sub I wrote, and I create a new instance of a
          UserControl:
          >
          sub??? You mean method right? :)
          >
          >
          >
          ctrlAPs tempctrl = new ctrlAPs();
          >
          Now, I would like to be able to use this sub I wrote for more than one
          UserControl, so I was trying to do something like this:
          >
          private void somesub(UserCon trol sourcectrl)
          {
          sourcectrl tempctrl = new sourcectrl();
          }
          But when I do that, I get the following error on compile:
          "The type or namespace name 'sourcectrl' could not be found (are you
          missing a using directive or an assembly reference?)"
          >
          So, I'm obviously not passing this in right. There has to be a way to
          do this, as I do it with panels and other controls. Any help is
          appreciated.
          >
          Why are you creating the UserControl inside the method?
          What decide which UserControl to create?
          How you return the new instance to the calling code?
          >
          What if you create the user control outside and pass it to the method?

          Comment

          • cody

            #6
            Re: Passing Items into a sub

            Nicholas Paldino [.NET/C# MVP] wrote:
            Chris,
            >
            You could use Generics, like so:
            >
            private void somesub<T>(T sourcectrl) where T : UserControl, new()
            {
            T tempctrl = new sourcectrl();
            }
            >
            This will allow you to use the type parameter T and pass in any type
            that derives from UserControl as well as has a default, parameterless
            constructor.
            >
            In the method, you will only be able to call methods that exist on the
            UserControl class, nothing that is derived from it.
            >
            You mean

            private void somesub<T>() where T : UserControl, new()
            {
            T tempctrl = new T();
            }

            :)

            Comment

            • Nicholas Paldino [.NET/C# MVP]

              #7
              Re: Passing Items into a sub

              No, I really meant what I typed. I know it doesn't ^do^ anything, but
              that's the OP's issue to figure out. I'm assuming the OP has the
              prerequisite knowledge to include a return value if needed.


              --
              - Nicholas Paldino [.NET/C# MVP]
              - mvp@spam.guard. caspershouse.co m

              "cody" <deutronium@gmx .dewrote in message
              news:%23aYUPMY6 HHA.1484@TK2MSF TNGP06.phx.gbl. ..
              Nicholas Paldino [.NET/C# MVP] wrote:
              >Chris,
              >>
              > You could use Generics, like so:
              >>
              >private void somesub<T>(T sourcectrl) where T : UserControl, new()
              >{
              > T tempctrl = new sourcectrl();
              >}
              >>
              > This will allow you to use the type parameter T and pass in any type
              >that derives from UserControl as well as has a default, parameterless
              >constructor.
              >>
              > In the method, you will only be able to call methods that exist on
              >the UserControl class, nothing that is derived from it.
              >>
              >
              You mean
              >
              private void somesub<T>() where T : UserControl, new()
              {
              T tempctrl = new T();
              }
              >
              :)

              Comment

              • Tom Porterfield

                #8
                Re: Passing Items into a sub

                Nicholas Paldino [.NET/C# MVP] wrote:
                No, I really meant what I typed. I know it doesn't ^do^ anything, but
                that's the OP's issue to figure out. I'm assuming the OP has the
                prerequisite knowledge to include a return value if needed.
                Look again at the correction that was made as it was not about return
                values. You created the local variable from the passed in parameter:

                T tempctrl = new sourcectrl();

                The correction was to create the local variable from the generic type:

                T tempctrl = new T();

                If you meant to create the local variable from the passed in parameter
                then I'll need some education on how that works especially in instances
                where sourcectrl is null.
                --
                Tom Porterfield

                Comment

                • Nicholas Paldino [.NET/C# MVP]

                  #9
                  Re: Passing Items into a sub

                  Ahh, got it.


                  --
                  - Nicholas Paldino [.NET/C# MVP]
                  - mvp@spam.guard. caspershouse.co m

                  "Tom Porterfield" <tpporter@mvps. orgwrote in message
                  news:%236wL0jY6 HHA.5096@TK2MSF TNGP04.phx.gbl. ..
                  Nicholas Paldino [.NET/C# MVP] wrote:
                  > No, I really meant what I typed. I know it doesn't ^do^ anything,
                  >but that's the OP's issue to figure out. I'm assuming the OP has the
                  >prerequisite knowledge to include a return value if needed.
                  >
                  Look again at the correction that was made as it was not about return
                  values. You created the local variable from the passed in parameter:
                  >
                  T tempctrl = new sourcectrl();
                  >
                  The correction was to create the local variable from the generic type:
                  >
                  T tempctrl = new T();
                  >
                  If you meant to create the local variable from the passed in parameter
                  then I'll need some education on how that works especially in instances
                  where sourcectrl is null.
                  --
                  Tom Porterfield

                  Comment

                  • Chris

                    #10
                    Re: Passing Items into a sub

                    Thanks folks- Nicholas' suggestion with Marc's / Tom's modification
                    did the trick!

                    On Aug 28, 11:47 am, "Nicholas Paldino [.NET/C# MVP]"
                    <m...@spam.guar d.caspershouse. comwrote:
                    Ahh, got it.
                    >
                    --
                    - Nicholas Paldino [.NET/C# MVP]
                    - m...@spam.guard .caspershouse.c om
                    >
                    "Tom Porterfield" <tppor...@mvps. orgwrote in message
                    >
                    news:%236wL0jY6 HHA.5096@TK2MSF TNGP04.phx.gbl. ..
                    >
                    Nicholas Paldino [.NET/C# MVP] wrote:
                    No, I really meant what I typed. I know it doesn't ^do^ anything,
                    but that's the OP's issue to figure out. I'm assuming the OP has the
                    prerequisite knowledge to include a return value if needed.
                    >
                    Look again at the correction that was made as it was not about return
                    values. You created the local variable from the passed in parameter:
                    >
                    T tempctrl = new sourcectrl();
                    >
                    The correction was to create the local variable from the generic type:
                    >
                    T tempctrl = new T();
                    >
                    If you meant to create the local variable from the passed in parameter
                    then I'll need some education on how that works especially in instances
                    where sourcectrl is null.
                    --
                    Tom Porterfield

                    Comment

                    Working...