让方法处理“无限”的方法。参数个数? (C#) [英] Have a method handle an "infinite" number of parameters? (C#)

查看:64
本文介绍了让方法处理“无限”的方法。参数个数? (C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查一堆文本框中是否有空,(根据情况,它们可以是5或50或500;这是无关紧要的)。



我写了一个简单的方法来检查文本框是否为空:

I'm needing to check if any of a bunch of textboxes are empty, (they can be 5 or 50 or 500 depending on the situation; tho this is irrelevant).

I wrote a simple method to check if a textbox is empty:

private bool IsEmpty(TextBox checkthis)
{
    if (checkthis.Text.Length == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}





然而,这需要我这样做:



This, however, requires me to do something like this:

if(IsEmpty(txtBxFirstName) || IsEmpty(txtBxLastName) || IsEmpty(txtBxDOB))
{
    Something();
}





手动输入几个没问题,但这样做的方式,我需要更多的东西多功能。



我想做的是从一个对象到数百个对象中的任何东西作为参数以某种等效的方法。



任何人都有任何想法?



It's no problem manually typing a few, but the way this is shaping up to be, I need something more versatile.

What I want to do is to have anything from 1 object to hundreds of objects as parameters in some equivalent method.

Anybody got any ideas??

推荐答案

除了需要改进的解决方案1:



更简单,您可以在常规数组中收集一组文本框或其他控件:
In addition to Solution 1, which needs to be improved:

Simpler, you can have a set of text boxes or other controls collected in a regular array:
TextBox[] textBoxes = new TextBox[] { txtBxFirstName, txtBxLastName, txtBxDOB, };
//and then, for example
foreach(TextBox texBox in textBoxes) { /* whatever... */ } 



你需要创建一个 textBoxes 只有当你打算在其他地方使用它时,在about循环或LINQ中,否则你将在循环中创建数组。该集在开发期间是可变的,但在运行期间是恒定的,因此使用集合将是多余的。



但是如果你没有这样看起来很好列表/集合中的控件太多。如果有太多,它将变得无法管理,根本原因是:为什么你会在设计师中创建所​​有这些控件?很多人错误地认为设计师是一种自动化的开发,但事实上它可能只是很多手工工作,不利于维护。如果你有一大堆按钮,把它们放在一个设计师,照顾对齐和其他事情将是愚蠢的。手动编写XAML会更快(复制和过去;-)),但不是更合理。主要的是:你真的不需要这么多按钮的单独名称,你需要通过像 textBoxes 这样的数组对象来解决所有问题。你宁愿需要先创建一个循环。类似于:


You need to create a textBoxes object only if you are going to use it elsewhere, in about loop or LINQ, otherwise you would create the array right in the loop. The set is variable during the development, but is constant during run-time, so using a collection would be redundant.

But this would only look fine if you have not too many controls in a list/collection. If there are too many, it will became unmanageable, and the root cause would be: why would you create all those controls in a designer at all? So many mistakenly think that the designer is a kind of automation of development, but in fact it can be just a lot of manual work, bad for maintenance. If you have a big stack of buttons, putting them in a designer, taking care of alignment and other things would be just silly. Writing XAML by hand would be faster (copy and past ;-)), but not more reasonable. Main thing is: you don't really need individual names for so many buttons, you rather need to address then all via an array object like textBoxes. You rather need to create then in a loop in first place. Something like:

Grid parent = someGrid; // someGrid already existed in XAML
// create, say, row definitions
TextBox[] textBoxes = new TextBox[count]; // somewhere you know what is the count
for (int index; index < count; ++index) {
    textBoxes[index] = new TextBox();
    // take care of all its properties...
    // insert it:
    parent.Children.Add(textBoxes[index]);
    // and, this is a main question often asked on WPF:
    // how to set a dependency property of a child control?
    // here is how:
    parent.SetRow(textBoxes[index], index);
    // and so on...
}





-SA


作为解决方案1和3的补充,以下提示可能如下很有用。



你可以拥有一个接受任意数量参数的函数:

In complement to solutions 1 and 3, the following tips might be useful.

You can have a functions that accept an arbitrary number of parameters:
void DoSomething(params TextBox[] textBoxes)
{
    foreach (var item in textBoxes) { /* Do something */ }
}



这个可以这样使用:


This can be used like that:

DoSomething(txtBxFirstName, txtBxLastName, txtBxDOB);





请参阅MSDN文档: http://msdn.microsoft.com/fr -fr / library / w5zay9db.aspx [ ^ ]



如果您只有几个函数可以调用许多控件,这将非常有用。如果您有许多不同的事情要做,那么在解决方案1或常规循环中使用lambdas可能是合适的。它还取决于控件的复杂程度。



正如解决方案3中所提到的,在某些情况下,直接从中创建项目可能是有意义的。数组并避免设计师。我认为如果你在问题中提到100个控件那么它可能是一个很好的解决方案。



另一方面,在某些对话框中你可能会喜欢10到20个控件(比如在多个字段中输入名称和地址的表单)。在这种情况下,您可能仍希望在设计器中布局控件,但使用数组(显式或隐式使用params关键字)可能有意义。



See MSDN documentation: http://msdn.microsoft.com/fr-fr/library/w5zay9db.aspx[^]

This can be useful if you have only a few function to call of many controls. If you have many different things to do, then using lambdas as in solution 1 or regular loop might be appropriate. It also depends on the complexity of what is done with the controls.

As mentionned in solutions 3, in some cases, it might make sense to create items directly from the array and avoid the designer. I would think that if you have 100 controls as mentionned in the question then it might be a good solution.

On the other hand, in some dialogs you might have like 10 to 20 controls (say a form to enter name and address in multiples fields). In such case, you might still want to layout controls in the designer but using array (either explicitly or implicitly with params keyword) might make sense.


//create List or array of text boxes 
List<TextBox> textboxes = new List<TextBox>(){txtBxFirstName, txtBxLastName, txtBxDOB}; // you can add any number of textbox controls 
//then you can simply do as below 
if(textboxes.Any(t=>string.IsNullOrEmpty(t.Text))
{
    Something();
}



或你可以创建如下方法


or you can create method like below

public void DoSomething(List<TextBox> textboxes)
{
    if(textboxes.Any(t=>string.IsNullOrEmpty(t.Text))
    {
        Something();
    }
}


这篇关于让方法处理“无限”的方法。参数个数? (C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆