开发者

Type of params in c# methods

开发者 https://www.devze.com 2023-03-14 15:09 出处:网络
I have a method public string GetValue(TextBox txt, DropdownList ddl) { if(txt != null) return txt.text; else

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. How can I resolve this issue? Dynamically i call that method and at a time either TextBox or DDl exists. So based on that i have to return value from That Control.

I am getting some error message as Method has invalid arguments wh开发者_StackOverflow社区en i pass Null.


I can only pass either textbox or ddl and null for the other param. 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;

OR you have

var value = GetValue(null, ddlMyList);

which sets value to 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.

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. 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.)


if you want to pass many parameters you can use param keyword

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. 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 "";
}
0

精彩评论

暂无评论...
验证码 换一张
取 消