数组os类 [英] Array os Class
问题描述
我必须设计一个接收Control对象的方法,如果它是TextBox对象,则返回单个
值,如果是另一个值,则返回另一个值它是一个ComboBox对象
等等。
如果我尝试if-else-if循环,这将非常不开心!我可以使用foreach
来制作吗?如何声明和创建在运算符中使用的数组是
(用于测试控件,如cont is TextBox)?
谢谢,
Max
MaxAndréBündchen写道:你好,
我必须设计一个接收Control对象的方法,如果它是TextBox对象则返回单个
值,如果它是ComboBox对象则返回另一个值
等等。
如果我尝试if-else-if循环,这将非常不开心!我可以使用foreach
来制作吗?如何声明和创建在运算符中使用的数组是
(用于测试控件,如cont is TextBox)?
谢谢,
Max
你不能使用foreach来处理这个列表中的所有* textboxes *,
并忽略其他控件,如果这就是你的意思。
我认为你不能使用重载在方法上,当你提供
a * Control *(恰好是一个TextBox)而不是真实的 TextBox。
所以我认为你会遇到一堆if',可能是沿着
行:
>
私有字符串ControlValue(控制myComtrol)
{
TextBox tb = myControl as TextBox;
if(tb!= null )
返回tb.Text;
ComboBox cbx = myControl as ComboBox;
if(cbx!= null)
返回cbx.SelectedValue; //或者任何正确的属性
//等等
}
-
Hans Kesting
Hans,
我认为使用是是是更好的选择。
如果(_control是TextBox)。
{
//你做的东西
}
我同意其余的。如果您运行foreach在控制集合上,
你将需要拥有与你支持的控制类型数量一样多的foreach。这是一种更好,更快的方式。
Ranjan。
-
http://dotnetjunkies.com/weblog/dotnut
Hans Kesting <是ne *********** @ spamgourmet.com>在消息中写道
新闻:Ow ************** @ TK2MSFTNGP11.phx.gbl ...MaxAndréBündchen写道:
我必须设计一个接收Control对象的方法,如果它是TextBox对象则返回单个
值,如果是,则返回另一个值ComboBox
对象等等。
如果我尝试if-else-if循环,这将非常不开心!我可以使用
foreach来做到这一点吗?如何声明和创建数组以在运算符
"中使用是 (用于测试控件,比如cont is TextBox)?
谢谢,
>>
如果这就是你的意思,你不能将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
>如果我尝试if-else-if循环,这将非常不开心!我可以使用
foreach
:-)
"MaxAndréBündchen" <>写在消息
新闻:OC ************** @ TK2MSFTNGP10.phx.gbl ...>
我必须设计一个接收Control对象的方法,如果它是TextBox对象则返回单个值,如果它是ComboBox对象则返回另一个值
等等。
如果我尝试if-else-if循环,这将非常不开心!我可以使用
foreach来做到这一点吗?如何声明和创建数组以在运算符
"中使用是 (用于测试控件,比如cont is TextBox)?
谢谢,
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 André Bündchen wrote: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 Kesting
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.
--
http://dotnetjunkies.com/weblog/dotnut
"Hans Kesting" <ne***********@spamgourmet.com> wrote in message
news:Ow**************@TK2MSFTNGP11.phx.gbl...Max André Bündchen wrote: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 Kesting
> If I try a "if-else-if" loop, this will be very unhappy! Can I use a
foreach
:-)
"Max André Bündchen" <.> wrote in message
news:OC**************@TK2MSFTNGP10.phx.gbl...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
这篇关于数组os类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!