Array os Class

  • Thread starter Thread starter Max André Bündchen
  • Start date Start date
M

Max André Bündchen

Hi,

I must design a method that receive a Control object and return a single
value if it is a TextBox object, another value if it is a ComboBox object
and so on.

If I try a "if-else-if" loop, this will be very unhappy! Can I use a foreach
to make that? How declare and create the array for use in the operator "is"
(for test a Control, like "cont is TextBox")?

Thanks,

Max
 
Max said:
Hi,

I must design a method that receive a Control object and return a single
value if it is a TextBox object, another value if it is a ComboBox object
and so on.

If I try a "if-else-if" loop, this will be very unhappy! Can I use a foreach
to make that? How declare and create the array for use in the operator "is"
(for test a Control, like "cont is TextBox")?

Thanks,

Max

You can't use foreach for things like "all *textboxes* in this list,
and ignore other controls", if that is what you mean.

I don't think you can use "overloading" on the method, as you supply
a *Control* (that happens to be a TextBox) instead of a "real" TextBox.

So I think you are stuck with a bunch of if's, probably along the
lines of:

private string ControlValue(Control myComtrol)
{
TextBox tb = myControl as TextBox;
if (tb != null)
return tb.Text;

ComboBox cbx = myControl as ComboBox;
if (cbx != null)
return cbx.SelectedValue; // or whatever the correct property is

// etc.
}
 
Hans,
I think using "is" is the better option.
if(_control is TextBox) .
{
//do ur stuff
}

I agree with the rest. If you run a "foreach" over the control collection,
you will have to have as many foreaches as the number of Control Types you
are supporting. This is a better and a faster way.

Ranjan.
 
Hi,

Yes, you can do it.
Just create a new struct like this:
struct XXX
{
Type ControlType;
string Value;
}

create a collection of it with the controls/values you want to add

ArrayList ar = new ArrayList()

ar.Add( new XXX( typeof( System.Windows.Forms.TextBox, "textbox);
.....

the later you can do your method like this:

string GetValue( object o )
{
foreach( XXX x in ar)
if ( x.ControlType == o.GetType() )
return x.Value;
}


You could do it even without the foreach if you use a Hashtable instead of a
ArrayList

Cheers,
 
Ignacio,
You still have to have the "if" statements. So whatever you have said,
though technically correct, is an overkill i am afraid.
Ranjan
 
Hi,

The IF ( or a comparision) will never dissapear, you could only hide it.
even if you use a Hashtable , use object.GetType().ToString() as the key
internally the Hashtable will do a IF

Your code may looks much cleaner though.

Cheers,
 
Hans said:
TextBox tb = myControl as TextBox;
if (tb != null)
return tb.Text;
I think using "is" is the better option.
if(_control is TextBox) .
{
//do ur stuff
}

I'm reading Jeffrey Richter's "Applied Microsoft .NET Framework Programming"
right now, and he makes an argument on pages 119 and 120 that "as" is a
better option than "is". The argument goes something like this. Each time
you perform an "is", an "as" or a cast, the run-time checks the types
involved for compatibility, and that costs time. When you do an "is", type
checking typically needs to occur twice:

if(myControl is TextBox) //type checking occurs here...
{
TextBox myTextBox=(TextBox) myControl; //...and again here!
//do stuff with myTextBox...
}

Whereas if you do an "as", a type check only occurs once:

TextBox myTextBox=(myControl as TextBox) //type checking occurs here...
if(myTextBox!=null) //...but not here!
{
//do stuff with myTextBox...
}

So theoretically at least, code is more efficient with "as" than with "is"
plus a cast, because a type compatibility check takes more time than simple
null-reference check.

This is probably not very helpful to the person who asked the original
question, but I thought I'd share the point anyway :-)
 
Hans,

Funny,
I don't think you can use "overloading" on the method, as you supply
a *Control* (that happens to be a TextBox) instead of a "real" TextBox.
You reply on an answer in the general newsgroup on this multiposted message.

Cor
 
Back
Top