Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7544401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:39:46+00:00 2026-05-30T08:39:46+00:00

I have a method that needs to allow a wide variety of input types.

  • 0

I have a method that needs to allow a wide variety of input types. There are two categories of arguments: boundary type arguments, and actual data input arguments.

The boundary arguments are for example, order, frequency, number of points, and number of data points. These boundary arguments are of type int and are common, regardless of the actual data input argument type.

The actual input arguments may be of types: byte, int, short, uint, float, double, decimal, long, etc. To complicate matters further, the actual input data may be individual data, or a list or array of that type. So, the actual input may come as List or uint[], etc. This input is ultimately converted to type double – either as single data or double[].

The method consists of three parts: part one checks the validity of the boundary arguments. This part always applies regardles of the actual input data type. Part two checks and processes the input data arguments. This part changes depending of the type of the input data. Part three performs calculations on the data and is once again common.

I have thought about generic, and I have thought about standard overloading with generics but both seem inefficient. I have come up with what I believe is a workable solution but would appreciate comments: Is my approach computationally efficent, and is there a better way to do this. Your comments would be appreciated.

This is what I currently have:

// ... create lists to store data
static List<double> aList = new List<double>(8);
static List<double> fList = new List<double>(8);

public static double[] MyMethod(int numPts, int numData, object aValue, object fValue)
{
    // ... part 1
    if (numData < 2) throw new ArgumentOutOfRangeException("numberData must be >= 2.");
    if (numPts < 2) throw new ArgumentOutOfRangeException("numberPoints must be >= 2.");
    if (numData < numPts) throw new ArgumentOutOfRangeException("numberData must be
        >= numPts.");

    // ... part 2
    if (aValue is byte || aValue is short || aValue is int || aValue is long ||
        aValue is float || aValue is double || aValue is decimal ||
        aValue is List<byte> || aValue is byte[] || aValue is List<short> ||
        aValue is short[] || aValue is List<int> || aValue is int[] ||
        aValue is List<float> || aValue is float[] || aValue is List<double> ||
        aValue is double[])
    { }
    else throw new ArgumentException("a values must be of a numeric type.");

    double a = 0.0;
    if (aValue is byte || aValue is short || aValue is int || aValue is long ||
        aValue is float || aValue is double || aValue is decimal)
    {
        a = (double)aValue;
        // ... store individual values
        aList.Add(a);

        // ... create the x variable vector
        double[] x = aList.ToArray();     // a values
    }
    else if (aValue is List<byte> || aValue is List<short> || aValue is List<int> ||
             aValue is List<float> || aValue is List<double>)
    {
        // ... get the runtime type of the aValue object
        Type t = aValue.GetType();
        // ... convert the aValue object to a generic list
        IList tmp = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(t));
        // ... convert the generic list to a strongly typed list
        List<double> aValuesList = tmp.Cast<double>().ToList();

        // ... create the x variable vector
        double[] x = aValuesList.ToArray();     // a values
    }
    else
    {
        // ... process the vector input
        // ... get the runtime type of the aValue object
        Type t = aValue.GetType();
        // ... convert the aValue object to an array
        Array tmp = Array.CreateInstance(typeof([]).MakeGenericType(t), aValue.Length);
        // ... convert the generic array to a strongly typed array
        double[] x = tmp.Cast<double>().ToArray();
    }

    // ... part 3
    {
        // ... do calculations
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:39:47+00:00Added an answer on May 30, 2026 at 8:39 am

    Just make it accept a double[]. Let the calling code massage their data into the correct format, or provide overloads for the other datatypes.

    As an example, if your method is:

    public double[] Calculate(double[] aValue, double[] fValue, ...)
    {
    }
    

    You can provide overloads like:

    public double[] Calculate(double aValue, double fValue, ...)
    {
        return Calculate(new double[]{aValue}, new double[]{fValue}, ...);
    }
    
    public double[] Calculate(IEnumerable<double> aValue, IEnumerable<double> fValue, ...)
    {
        return Calculate(aValue.ToArray(), fValue.ToArray(), ...);
    }
    

    To cover other datatypes you can reduce the number of overloads by using something like:

    public double[] Calculate<T>(T aValue, T fValue) where T : IConvertible
    {
        return Calculate(aValue.ToDouble(), fValue.ToDouble(), ...);
    }
    
    public double[] Calculate<T>(IEnumerable<T> aValue, IEnumerable<T> fValue) where T : IConvertible
    {
        return Calculate(aValue.Select(x=>x.ToDouble()), fValue.Select(x=>x.ToDouble()), ...);
    }
    

    This should cover all other primitive data types, which is all of your example.

    If you do this, the code in your calculate method is reduced down to:

    public double[] Calculate(double[] aValue, double[] fValue, int numData, int numPts)
    {
        if (numData < 2) throw new ArgumentOutOfRangeException("numberData must be >= 2.");
        if (numPts < 2) throw new ArgumentOutOfRangeException("numberPoints must be >= 2.");
        if (numData < numPts) throw new ArgumentOutOfRangeException("numberData must be
        >= numPts.");
    
        // do calculation
    }
    

    … which is a lot simpler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method that needs to accept an array of country names, and
I have a method in my VBA code that needs to be assigned to
I have an action method that depending on some conditions needs to return a
I have an extension method that needs to return an HtmlString. The method has
I have a JavaScript method that I need to run on one of my
I have method in a class that I need to make sure is only
I have this method that changes an larger image source on click. I need
I have a method that takes an array of queries, and I need to
I need a Regex that will match a java method declaration. I have come
If I have an object that holds the parameters for my method. I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.