Overloading or something else?

T

Tetsuya Oguma

Hi all,

I just started coding C# migrating from VB/VBA and stumbled at this one.

A fired up KeyUp event of a control calls the below with the same "object
sender" parameter.

This "object sender" could be either TextBox or ComboBox. How can I get to
their Tag property that stores either *true* or *false*? I try to cast sender
but in vain...

private List<string> CoarseSourceToTickers(object sender, bool
isMarket1)
{
object control;

if (sender is BlpTextBox)
{
BlpTextBox control = (BlpTextBox)sender;
isMarket1 = System.Convert.ToBoolean(control.Tag);
}
else
{
BlpComboBox control = (BlpComboBox)sender;
isMarket1 = System.Convert.ToBoolean(control.Tag);
}

This gives me an error of "Error 1 A local variable named 'control' cannot
be declared in this scope because it would give a different meaning to
'control', which is already used in a 'parent or current' scope to denote
something else" with a wavy blue underline under the word, control in
"BlpTextBox control = (BlpTextBox)sender;"

Thanks!
 
M

Michael C

Tetsuya Oguma said:
Hi all,

I just started coding C# migrating from VB/VBA and stumbled at this one.

A fired up KeyUp event of a control calls the below with the same "object
sender" parameter.

This "object sender" could be either TextBox or ComboBox. How can I get to
their Tag property that stores either *true* or *false*? I try to cast
sender
but in vain...

private List<string> CoarseSourceToTickers(object sender, bool
isMarket1)
{
object control;

Take this line out, you've declared "control" 3 times. The first time as
object, the second time as BlpTextBox and third as BlpComboBox. The second
and third are ok because they are in different blocks but the first is going
to conflict.

Btw, what is BlpTextBox and BlpComboBox? Are these a custom usercontrol that
you have written or just the name you've given the controls on the form? If
the second then what you've done won't work, you need to define them as
plain old TextBox and ComboBox. However, I think you can just do this:

if(sender is Control) //<---- this is not really required. If you only
assign this event to a combo and textbox then you don't need this line.
{
Control control = (Control)sender;
isMarket = System.Convert.ToBoolean(control.Tag);
}

Both combobox and textbox inherit from control so you can define either as
control.

Michael
 

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

Similar Threads

CSharp Coding Standards 18
Asp.net Important Topics. 0

Top