Reputation: 32758
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, string pr, string fld, Int32 intVal = null, double dblVal = null, string strVal = null)
{
Depending on the value of fld I would want to update a certain field name in my table.
Is there some way I could code up the function parameters so I would not have to supply a long list of possible value types?
Ideally I would like to be able to call this function like this:
Update("key1", "key2", "location", "London");
Update("key1" , "key2", "beds", 2);
Update("key1" , "key2", "price", 123.45);
Then inside the function update I would like to have:
public void Update(string ac, string pr, xxx, yyy)
{
try
{
vm.Product = _product.Get(ac, pr);
vm.Product.xxx = yyy;
_product.AddOrUpdate(vm.Product);
}
catch (Exception e) { log(e); }
}
In the xxx I would like to give the field name and yyy the actual value. Maybe inside the function I could do some kind of casting with a case method based on the field name. I only have a small number of field names so that could be easy. Also maybe there is a way I could find the datatype from reflection of the vm class but that's far beyond my knowledge.
Upvotes: 1
Views: 350
Reputation: 83358
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);
Upvotes: 4
Reputation: 2839
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.
Upvotes: 1
Reputation: 14915
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)
{
.
.
}
Upvotes: 1
Reputation: 185593
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)
{
}
Upvotes: 1