简体   繁体   English

C#方法中的参数类型

[英]Type of params in c# methods

I have a method 我有办法

public string GetValue(TextBox txt, DropdownList ddl)
{
    if(txt != null)
        return txt.text;
    else
        return ddl.SelectedValue;
}

I can only pass either textbox or ddl and null for the other param. 我只能传递textbox或ddl,而其他参数则传递null。 How can I resolve this issue? 我该如何解决这个问题? Dynamically i call that method and at a time either TextBox or DDl exists. 动态地我调用该方法,并且一次TextBox或DDl存在。 So based on that i have to return value from That Control. 因此,基于此,我必须从That Control返回值。

I am getting some error message as Method has invalid arguments when i pass Null. 我通过Null时由于Method具有无效的参数而收到一些错误消息。

I can only pass either textbox or ddl and null for the other param. 我只能传递textbox或ddl,而其他参数则传递null。 How can I resolve this issue? 我该如何解决这个问题?

If this is the business rule you want to enforce, you don't resolve it, you change it. 如果这是您要强制执行的业务规则,则无需解决,您可以对其进行更改。 Use method overloading to only accept the type of parameter you actually want to use. 使用方法重载仅接受您实际要使用的参数类型。

public string GetValue(TextBox tb)
public string GetValue(DropDownList ddl)

I don't think you should be doing a method like this. 我不认为您应该这样做。

From the signature you have some code like 从签名中,您可以找到一些类似的代码

var value = GetValue(txtMyText, null);

which sets value to txtMyText.Text; 将值设置为txtMyText.Text;

OR you have 或者你有

var value = GetValue(null, ddlMyList);

which sets value to ddl.SelectedValue; 将值设置为ddl.SelectedValue;

The problem here is that this removes readability. 这里的问题是,这消除了可读性。 When I'm reading your code I see that you're doing a GetValue() but I'm at a loss for why you're passing in null in some parameters. 当我阅读您的代码时,我看到您正在执行GetValue(),但是对于为什么要在某些参数中传递null感到迷惑。

It's actually fairly clear, when reading code to just see: 实际上,在阅读代码时,很清楚:

var value = txtMyTextBox.Text;
var dropDownValue = ddlMyList.SelectedValue;

Making this method isn't really all that useful, because you have to handle each and every control type. 使用这种方法并不是真正有用的,因为您必须处理每种控件类型。 The classes already have methods to get their value, and you trying to write a utility method that will get a value regardless of class type obscures what is really going on with little value. 这些类已经具有获取其值的方法,并且您尝试编写一个实用程序方法,无论该类的类型如何,该方法都将获取值,从而使几乎没有什么值的实际情况变得晦涩。

Further, if you add more types to this method, you'll end up doing an If/Else until you find the type, and return the value. 此外,如果向此方法添加更多类型,最终将执行If / Else,直到找到类型,然后返回值。 This causes unnecessary CPU cycles, especially since you will already know the type at design time (since you're passing in null for one parameter.) 这会导致不必要的CPU周期,尤其是因为您在设计时就已经知道类型(因为为一个参数传递了null)。

if you want to pass many parameters you can use param keyword 如果要传递许多参数,可以使用param关键字

public string GetValue(params object[] controls)
{
    foreach (var item in controls)
    {
        if (item != null)
        {
            if (item is TextBox)
                return (item as TextBox).Text;
            if (item is CheckBox)
                return (item as CheckBox).Checked.ToString();

            if (item is DropDownList)
                return (item as DropDownList).SelectedValue.ToString();
        }
    }
    return string.Empty;
}

and call the method as this 然后像这样调用方法

            GetValue(TextBox1);
            GetValue(DropDownList1);
 GetValue(CheckBox1);
      GetValue(null,DropDownList1);
      GetValue(TextBox1,DropDownList1);
  GetValue(TextBox1,DropDownList1,CheckBox1);

Every Control inherits the class control . 每个控件都继承类control So one parameter is enough. 因此,一个参数就足够了。 You just have to determine the type: 您只需要确定类型:

public string GetValue(Control ctl) {
  if (ctl != null) {

    //Textbox
    if (ctl is TextBox) return ctl.Text;
    //Combobox
    if (ctl is ComboBox) {
      ComboBox cb = ctl as ComboBox;
      return cb.SelectedText;
    }
    //...
  }
  //Default Value (You can also throw an exception
  return "";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM