私有字符串ControlValue(控制myComtrol) { TextBox tb = myControl as TextBox; if(tb!= null ) 返回tb.Text; ComboBox cbx = myControl as ComboBox; if(cbx!= null) 返回cbx.SelectedValue; //或者任何正确的属性 //等等 } - Hans Kesting 如果这就是你的意思,你不能将foreach用于诸如此列表中的所有*文本框*,并忽略其他控件之类的内容。 我不是认为你可以使用重载在方法上,当你提供一个* Control *(恰好是一个TextBox)而不是一个真正的 TextBox。 所以我认为你会遇到一堆if',可能沿着以下几行: 私有字符串ControlValue(Control myComtrol) {TextBox tb = myControl as TextBox; if(tb!= null) return tb.Text; ComboBox cbx = myControl as ComboBox ; if(cbx!= null)返回cbx.SelectedValue; //或者任何正确的财产 // } - Hans Kesting Hi,I must design a method that receive a Control object and return a singlevalue if it is a TextBox object, another value if it is a ComboBox objectand so on.If I try a "if-else-if" loop, this will be very unhappy! Can I use a foreachto 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 supplya *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 thelines 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 Kesting 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 Kesting 这篇关于数组os类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-16 03:04