am i overloading this function properly?

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

    am i overloading this function properly?

    private void updateticks(Tex tBox textbox, CheckBox tickbox)
    {
    if (textbox.Text.L ength 0)
    {
    tickbox.Visible = true;
    }
    else
    {
    tickbox.Visible = false;
    }
    }
    private void updateticks(Com boBox combox, CheckBox tickbox)
    {
    if (combox.Text.Le ngth 0)
    {
    tickbox.Visible = true;
    }
    else
    {
    tickbox.Visible = false;
    }
    }

    ======
    the above works and i can pass either text box control or combo box
    control as the first argument when calling the function. But in code it
    looks like two different functions, is there anyway i can write the
    above to make it more obvious that it's one function with two
    overloaded sets of arguments?

  • DeveloperX

    #2
    Re: am i overloading this function properly?

    Well both TextBox and ComboBox are derived from Control, so you could
    have one method that takes a Control and a checkbox. Then you can check
    if it's a textbox or checkbox and extract the text into a variable and
    then set visible on the Checkbox at the end.

    Another alternative is to put the visible setting code into a third
    method and call that passing in the Text.


    CCLeasing wrote:
    private void updateticks(Tex tBox textbox, CheckBox tickbox)
    {
    if (textbox.Text.L ength 0)
    {
    tickbox.Visible = true;
    }
    else
    {
    tickbox.Visible = false;
    }
    }
    private void updateticks(Com boBox combox, CheckBox tickbox)
    {
    if (combox.Text.Le ngth 0)
    {
    tickbox.Visible = true;
    }
    else
    {
    tickbox.Visible = false;
    }
    }
    >
    ======
    the above works and i can pass either text box control or combo box
    control as the first argument when calling the function. But in code it
    looks like two different functions, is there anyway i can write the
    above to make it more obvious that it's one function with two
    overloaded sets of arguments?

    Comment

    • Ciaran O''Donnell

      #3
      RE: am i overloading this function properly?

      This is how overloading works. You could however use one function like :
      private void updateticks(Con trol control, CheckBox tickbox)
      {
      if (control.Text.L ength 0)
      {
      tickbox.Visible = true;
      }
      else
      {
      tickbox.Visible = false;
      }
      }

      as Text is inherited from that. If you want them together than a region
      around them can help with organising

      HTH

      Ciaran O'Donnell

      "CCLeasing" wrote:
      private void updateticks(Tex tBox textbox, CheckBox tickbox)
      {
      if (textbox.Text.L ength 0)
      {
      tickbox.Visible = true;
      }
      else
      {
      tickbox.Visible = false;
      }
      }
      private void updateticks(Com boBox combox, CheckBox tickbox)
      {
      if (combox.Text.Le ngth 0)
      {
      tickbox.Visible = true;
      }
      else
      {
      tickbox.Visible = false;
      }
      }
      >
      ======
      the above works and i can pass either text box control or combo box
      control as the first argument when calling the function. But in code it
      looks like two different functions, is there anyway i can write the
      above to make it more obvious that it's one function with two
      overloaded sets of arguments?
      >
      >

      Comment

      • Jeroen

        #4
        Re: am i overloading this function properly?

        Well, to me it already seems obvious enough, but probably I don't
        understand well enough what you mean. You said:
        control as the first argument when calling the function. But in code it
        looks like two different functions
        It not only _looks_ like two different functions, they actually _are_
        two different functions. They just have the same name and scope. So you
        can either (A) change the fact that they are two different functions by
        DeveloperX's suggestion or (B) perhaps try to group them just a little
        more:


        [ A ]
        private void updateticks(Con trol mycontrol, CheckBox tickbox)
        {
        if (mycontrol is TextBox) ....
        else if (mycontrol is ComboBox) ...
        }


        [ B ]
        #region updateticks overloads
        private void updateticks(Tex tBox textbox, CheckBox tickbox) ...
        private void updateticks(Com boBox combox, CheckBox tickbox)
        #endregion

        Hope that helps (a little :)).

        -Jeroen

        Comment

        • Jeroen

          #5
          Re: am i overloading this function properly?


          Ciaran O''Donnell wrote:
          ...
          LOL, double post, sorry, was writing my post when you submitted.

          Comment

          • Mark R. Dawson

            #6
            RE: am i overloading this function properly?

            Hi CCLeasing,
            probably the simplest way to do this if you are always going to make the
            CheckBox visible based on the length of a string would be to not pass in
            different types of controls, but just a string i.e.:

            private void UpdateTicks(str ing predicate, CheckBox tickBox)
            {
            tickBox.Visible = (predicate.Leng th 0);
            }

            then you are going to call this from other locations by:
            UpdateTicks(myT extBox.Text, myCheckBox);
            UpdateTicks(myC omboBox.Text, myCheckBox);

            so you do not have to re-write the logic each time for different types of
            controls since all you are using is the length of a string to determine the
            visibility of the tickBox.

            Mark.
            --



            "CCLeasing" wrote:
            private void updateticks(Tex tBox textbox, CheckBox tickbox)
            {
            if (textbox.Text.L ength 0)
            {
            tickbox.Visible = true;
            }
            else
            {
            tickbox.Visible = false;
            }
            }
            private void updateticks(Com boBox combox, CheckBox tickbox)
            {
            if (combox.Text.Le ngth 0)
            {
            tickbox.Visible = true;
            }
            else
            {
            tickbox.Visible = false;
            }
            }
            >
            ======
            the above works and i can pass either text box control or combo box
            control as the first argument when calling the function. But in code it
            looks like two different functions, is there anyway i can write the
            above to make it more obvious that it's one function with two
            overloaded sets of arguments?
            >
            >

            Comment

            • chanmm

              #7
              Re: am i overloading this function properly?

              It looks nothing wrong to me. If you can compile and produce the result you
              want then just use it.

              chanmm

              "CCLeasing" <gary@ccleasing .co.ukwrote in message
              news:1163000281 .016589.284400@ b28g2000cwb.goo glegroups.com.. .
              private void updateticks(Tex tBox textbox, CheckBox tickbox)
              {
              if (textbox.Text.L ength 0)
              {
              tickbox.Visible = true;
              }
              else
              {
              tickbox.Visible = false;
              }
              }
              private void updateticks(Com boBox combox, CheckBox tickbox)
              {
              if (combox.Text.Le ngth 0)
              {
              tickbox.Visible = true;
              }
              else
              {
              tickbox.Visible = false;
              }
              }
              >
              ======
              the above works and i can pass either text box control or combo box
              control as the first argument when calling the function. But in code it
              looks like two different functions, is there anyway i can write the
              above to make it more obvious that it's one function with two
              overloaded sets of arguments?
              >

              Comment

              Working...