1

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.

4
  • Could you provide some examples of how your method calls would manifest? Commented Dec 29, 2011 at 4:13
  • Can you always pass a string and then convert it within the function to an int or double (e.g. Int32.Parse(strVal)) or will that cause precision problems in some cases? Commented Dec 29, 2011 at 4:17
  • Yeah I think always passing a string would work. Inside the function I would then just need to convert to the required object type. I think that's a great way to go. Commented Dec 29, 2011 at 4:23
  • I'm not sure it's a great, or even good way to go...just an option. I suspect you will do better with Adam's way but let me ask, can't you just pass all three values into the function once in a custom class? Commented Dec 29, 2011 at 4:38

4 Answers 4

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);
Sign up to request clarification or add additional context in comments.

2 Comments

I always have the four arguments. Just not sure how to handle the coversion and choosing which one or datatype I should use.
there's I think a small error as it should be vm.Product and not vm in the setting. Also there's no datatype conversion. I'll mark this answered as your answer is I think the best. I opened up another question and specifically mentioned datatype conversion. Thanks.
1

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

}

2 Comments

Would it be possible to combine all these into one using "object" ? Then have the function just pick the appropriate cast?
You can certainly create an overload that takes object, but then you can pass anything. If your options are to take one of the three choices, then using overloads in this way is the right way to go. You can't just have one function to do this.
1

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

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.