1

I have method which process the double arrays. I am supplying double arrays using keyword params. My problem is sometimesI have to pass doubles as well and I want to use to same method for both types. Currently method have signature

public void DoSomething(params double[][] arrays)

Above method definition works fine for following arguments:

double [] array = new double []{2,3,5} ;
double [] anotherarray = new double []{7,8,10} ;
DoSomething (array, anotherarray) ;

I am thing to pass object and then cast them in this method and use try catch block and I do not know it is right approach or there exists some elegant way to handle this kind of situation because I can have mixed data as input arguments.

public void DoSomething(params object objs)
{   
   // for loop
   try 
   {
      var tempdata =  (double) objs(loop index);
      double[] data = new double[] { tempdata };
   } 
   catch 
   {
      var tempdata = (double []) objs(loop index);
      double [] data = tempdata;
   }
   // do other opertaion
}

I want to call this way:

double [] array = new double []{2,3,5} ;
double singlevalue = 10 ;
double [] anotherarray = new double []{7,8,10} ;
DoSomething (array, singlevalue, anotherarray) ;
8
  • Why don't you just overload the method? Commented Nov 7, 2014 at 9:48
  • I mentioned that my data can be mixed type and due to which I am looking this kind of functionality Commented Nov 7, 2014 at 9:48
  • @JonSkeet: if it does the same thing for say doubles and singles wouldn't you need to at some point call a common overload or either repeat all the code? (apologies if I am being slow on a friday morning here). Commented Nov 7, 2014 at 9:52
  • What exactly do you mean by "mixed data" here? It doesn't help that you haven't shown any examples of calling the method. If you sometimes have double[][] and sometimes have double[], but those are the only options, then I would definitely suggest adding an overload (and making one overload call the other). Commented Nov 7, 2014 at 9:54
  • @Chris: I haven't seen any indication that float is required yet - the OP isn't being very specific, but I can only see double[] and duoble[][] at the moment, which can definitely be handled with overloads. And yes, one overload would call the other, but that's fine. Commented Nov 7, 2014 at 9:54

1 Answer 1

2

Now you've given an example of how you want to call it, I suspect that using dynamic typing may be the simplest approach:

public void DoSomething(params object[] values)
{
    foreach (dynamic value in values)
    {
        // Overload resolution will be performed at execution time
        DoSomethingImpl(value);
    }
}

private void DoSomethingImpl(double value) { ... }

private void DoSomethingImpl(double[] value) { ... }

You could do it manually if you want:

public void DoSomething(params object[] values)
{
    foreach (object value in values)
    {
        if (value is double)
        {
            DoSomethingImpl((double) value);
            // Or: DoSomethingImpl(new[] { (double) value });
        }
        else if (value is double[])
        {
            DoSomethingImpl((double[]) value);
        }
        ...
    }
}

I would definitely not just cast unconditionally and then catch the exception that's thrown. That's a horrible abuse of exceptions.

Sign up to request clarification or add additional context in comments.

1 Comment

yes, I was not feeling comfortable with try catch block.I thought may be I could save few lines of code and however it seems difficult. Thanks for help.

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.