am i overloading this function properly?

  • Thread starter Thread starter CCLeasing
  • Start date Start date
C

CCLeasing

private void updateticks(TextBox textbox, CheckBox tickbox)
{
if (textbox.Text.Length > 0)
{
tickbox.Visible = true;
}
else
{
tickbox.Visible = false;
}
}
private void updateticks(ComboBox combox, CheckBox tickbox)
{
if (combox.Text.Length > 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?
 
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.
 
This is how overloading works. You could however use one function like :
private void updateticks(Control control, CheckBox tickbox)
{
if (control.Text.Length > 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
 
Well, to me it already seems obvious enough, but probably I don't
understand well enough what you mean. You said:

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(Control mycontrol, CheckBox tickbox)
{
if (mycontrol is TextBox) ....
else if (mycontrol is ComboBox) ...
}


[ B ]
#region updateticks overloads
private void updateticks(TextBox textbox, CheckBox tickbox) ...
private void updateticks(ComboBox combox, CheckBox tickbox)
#endregion

Hope that helps (a little :)).

-Jeroen
 
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(string predicate, CheckBox tickBox)
{
tickBox.Visible = (predicate.Length > 0);
}

then you are going to call this from other locations by:
UpdateTicks(myTextBox.Text, myCheckBox);
UpdateTicks(myComboBox.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.
 
It looks nothing wrong to me. If you can compile and produce the result you
want then just use it.

chanmm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top