locked
How to pass array as argument to a method without datatype RRS feed

  • Question

  • Friends,

    I would like to know how to pass array argument to a method without specifying its datatype(even without dynamic,var) that is one step. ( Solution can be in any lang like C#,VB.NET,WPF,Silverlight )

    Example :

    This is the actual method

     public void Test(double[] xLoc)
            {

            }

    I want to call this method like Test({2,3,4}) or any other way without specifying its dataype


    but not,  Test(new double[] {2,3,4})

    because user is going to call this method from my editor & user doesn't know new double[],new datetime[] .


    plz friends guide me any such feature added in C#,VB.NET,WPF,etc.. or VS 2012 ?

     


    Thanks - Ravi

    Tuesday, November 27, 2012 9:50 AM

Answers

  • Why not using object data type? And use some checking (if,else blocks) to find the correct data type, and convert to it.

    Mitja

    • Proposed as answer by Norkk Tuesday, November 27, 2012 10:57 AM
    • Marked as answer by Jason Dot Wang Thursday, December 6, 2012 2:38 AM
    Tuesday, November 27, 2012 10:31 AM
  • The closest thing you can do is to use params:

    public void Test(params double[] xLoc) { }

    Test(2,3,4)

    • Marked as answer by Jason Dot Wang Thursday, November 29, 2012 5:14 AM
    • Unmarked as answer by Jason Dot Wang Thursday, November 29, 2012 5:14 AM
    • Proposed as answer by Jason Dot Wang Thursday, November 29, 2012 5:15 AM
    • Marked as answer by Jason Dot Wang Thursday, December 6, 2012 2:38 AM
    Tuesday, November 27, 2012 10:22 AM

All replies

  • The closest thing you can do is to use params:

    public void Test(params double[] xLoc) { }

    Test(2,3,4)

    • Marked as answer by Jason Dot Wang Thursday, November 29, 2012 5:14 AM
    • Unmarked as answer by Jason Dot Wang Thursday, November 29, 2012 5:14 AM
    • Proposed as answer by Jason Dot Wang Thursday, November 29, 2012 5:15 AM
    • Marked as answer by Jason Dot Wang Thursday, December 6, 2012 2:38 AM
    Tuesday, November 27, 2012 10:22 AM
  • Why not using object data type? And use some checking (if,else blocks) to find the correct data type, and convert to it.

    Mitja

    • Proposed as answer by Norkk Tuesday, November 27, 2012 10:57 AM
    • Marked as answer by Jason Dot Wang Thursday, December 6, 2012 2:38 AM
    Tuesday, November 27, 2012 10:31 AM
  • Ravi -

    Declaring and calling the method are different things.

    Declare the method with datatype and overload the same method with different datatypes so that user can pass any kind of data.

    User is going to pass only value not datatype.


    Please mark it as an answer/helpful if you find it as useful. Thanks, Satya Prakash Jugran

    Tuesday, November 27, 2012 10:38 AM