How can I code a C# function to accept a variable number of parameters?

前端 未结 4 1021
谎友^
谎友^ 2021-01-23 11:21

I have a C# method that I would like to use to update some data. The method could be passed either a string, a double, an integer

public void Update(string ac, s         


        
相关标签:
4条回答
  • EDIT

    It looks like you want to pass in a property name, and a value. A little bit of reflection should make this easy:

    public void Update(string ac, string pr, string propertyName, object Value) {
        try {
            vm.Product = _product.Get(ac, pr);
            vm.Product.GetType().GetProperty(propertyName).SetValue(vm.Product, value, null);
            _product.AddOrUpdate(vm.Product);
        }
    }
    

    END EDIT

    A params array allow your method to accept a variable number of parameters

    public void Update(string ac, string pr, params object[] arguments)
    

    Which could be called with any of your examples above

    Update("key1", "key2", "location", "London");
    Update("key1" , "key2", "beds", 2);
    Update("key1" , "key2", "price", 123.45);
    
    0 讨论(0)
  • 2021-01-23 11:48

    The best way I could suggest you is replace all the parameters with an Object. All these parameters would represent some info with which you want to update your data base

    class UpdateInfo
    {
      public string ac {get; set;}
      public string pr {get; set;}
      public string fld {get; set;}
      .
      .
      .
    }
    

    All your validation logic for the each parameter could also go in here.

    public void Update(UpdateInfo obj)
    {
     .
     .
    }
    
    0 讨论(0)
  • 2021-01-23 11:55

    First of all, it seems you should pass all of these values in once, in a single custom class rather than making three separate calls. But if that's not what you want to or can do...

    If your various values can all be converted into strings before the function call, you can pass them in that way. In fact, if they are coming from textboxes, they may already be strings to start with. If your third parameter (e.g. "location", "beds") can definitively indicate the data type you want back, this should work, or you could pass the desired datatype in as well.

    If you need to pass in an integer value intVal:

    string strVal = intVal.ToString();
    Update(ac, pr, fld, strVal);
    

    Then inside your function, you will need to determine if the value you want to insert should be anything other than a string and convert it. I would recommend using Double.TryParse and Int32.TryParse to better trap errors.

    Last time...this is far from elegant but it can work and you indicated you might want to try it, so good luck and let me know if you run into any problems with this.

    0 讨论(0)
  • 2021-01-23 11:57

    If it can be passed one of those parameters, it doesn't sound like you want a variable number; it sounds like you want three different overloads:

    public void Update(string ac, string pr, string fld, Int32 intVal)
    {
    
    }
    
    public void Update(string ac, string pr, string fld, double dblVal)
    {
    
    }
    
    public void Update(string ac, string pr, string fld, string strVal)
    {
    
    }
    
    0 讨论(0)
提交回复
热议问题