Hey All

I'm trying to write a simple write .csv file function that can take multiple arrays of objects and write to a specified file. I understand that I can use params to pass an unspecified number of variables, but is it possible to use it to pass an unspecified number of arrays of objects? Any tips would be greatly appreciated!

Cameron

?Is it possible to use it to pass an unspecified number of arrays of objects?
Yes you can do this.

void methodName(params object []v){
   ...
  }

?Is it possible to use it to pass an unspecified number of arrays of objects?
Yes you can do this.

void methodName(params object []v){
   ...
  }

Thanks for your reply. I guess I'm trying to do something that would look like this normally:

void methodName(double[] var1, double[] var2, int[] var3){
   ...
  }

But pass an unspecified number of arrays of variables. Does this make sense? Thanks a lot.

So you are passing an unknown number of arrays to the method...are they of different data types? If so, how many different data types?
Also, what are you trying to achieve, if we know why you are trying to do this we may be able to offer a more efficient solution :)

Thank you for your reply Ryshad. Ideal world: The method will take an unspecified number of arrays of either ints, doubles, bools, strings. Then using TextWriter, it will write these objects to a file delimited by an ','. I guess I could make a class of some kind that I pass the arrays into one at a time. What do you suggest?

?Is it possible to use it to pass an unspecified number of arrays of objects?
Yes you can do this.

void methodName(params object []v){
   ...
  }

adatapost pretty much gave you the way to do this. Here is an example of manipulating the variable arguments of object should you pass a variable number of arrays:

void methodName(params object[] v)
        {
            // treat each (variable) argument as an array...
            foreach (object[] ary in v)
            {
                // process the current array argument...
                foreach (object o in ary)
                {
                    Console.Write(o);
                    Console.Write(","); // delimiter for each item "o" in array...
                }
                // Separate each array with newline:
                Console.WriteLine();
            }
        }

I didn't actually test the above code, but it does compile.

EDIT: there is a casting exception with the above....

Sorry, I got distracted before I could correct the last post... This should work:

public static void Test()
        {
            int[] intAry = { 1, 2, 3 };
            string[] strAry = { "1", "2", "3" };
            double[] dblAry = { 1.0, 2.0, 3.0 };
            ProcessArrays(intAry, strAry, dblAry);
        }
        public static void ProcessArrays(params object[] v)
        {
            // for each array in variable arguments...
            foreach (Array ary in v)
            {
                // for each item in this array...
                foreach (object o in ary)
                {
                    Console.Write(o);
                    Console.Write(",");
                }
                // Separate each array with newline:
                Console.WriteLine();
            }
        }

Yep, if you cant be sure of the type of each array then you will need to pass in an array of objects. Any time you know for sure the number and type of the variables you should use strongly typed collections and variables. But in this instance DdoubleD and adatapost have nailed it, by using an array you can pass multiple variables and by declaring them as type object you can store any type of array in it.

Remember to mark the thread as solved if your question has been answered :)

Thanks a lot. This does solve the problem of passing unspecified number of arrays. I've never used a foreach loop before, and with the example that has been provided it would write one array per line. However, what I'm trying to do is write a value from each array next to each other. Is there a way to index the object array that is passed? Thank you all for you help this far.

Thanks a lot. This does solve the problem of passing unspecified number of arrays. I've never used a foreach loop before, and with the example that has been provided it would write one array per line. However, what I'm trying to do is write a value from each array next to each other. Is there a way to index the object array that is passed? Thank you all for you help this far.

Not sure what you mean, but here is the for/next index version with the arrays accessed by index:

public static void ProcessArrays2(params object[] v)
        {
            // for each array in variable arguments...
            for (int i=0; i<v.Length; i++)
            {
                Array ary = (Array)v[i];

                // for each item in this array...
                for (int ii=0; ii<ary.Length; ii++)
                {
                    Console.Write(ary.GetValue(ii));
                    Console.Write(",");
                }
                // Separate each array with newline:
                Console.WriteLine();
            }
        }

If you wanted to write one item from each array on each line then you would need to swap the for loops around.
The code DdoubleD has given you will loop through each item in the first array, then each item in the second array.

The following is an adjustment to the code which will print the first item of each array, then the second item of each array, etc. I think thats what you intended. I have also put in checks to ensure that if any of the arrays are shorter it will still run.

private void button9_Click(object sender, EventArgs e)
        {
            int[] intAry = { 1, 2, 3, 4 };
            string[] strAry = { "1", "2", "3", "4" };
            double[] dblAry = { 1.0, 2.0, 3.0 };
            ProcessArrays2(intAry, strAry, dblAry);
        }

        public static void ProcessArrays2(params object[] v)
        {
            //find length of longest array
            int highestLength = 0;
            foreach (Array a in v)
            {
                if (a.Length > highestLength)
                    highestLength = a.Length;
            }

            // for each index in item arrays...
            for (int i = 0; i < highestLength; i++)
            {
                string Line = "";

                // for each array...
                foreach (Array ary in v)
                {
                    if (i < ary.Length)
                        Line += ary.GetValue(i).ToString()+",";
                }

                //strip last ','
                if (Line.Length > 0)
                    Line = Line.Remove(Line.Length - 1);

                // Output line
                Console.WriteLine(Line);
            }
        }

What are you using this code for, i cant help but think there is probably a much more elegant solution. If you let us know what this is for we can probably show you a better way to do it :)

Not sure what you mean, but here is the for/next index version with the arrays accessed by index:

public static void ProcessArrays2(params object[] v)
        {
            // for each array in variable arguments...
            for (int i=0; i<v.Length; i++)
            {
                Array ary = (Array)v[i];

                // for each item in this array...
                for (int ii=0; ii<ary.Length; ii++)
                {
                    Console.Write(ary.GetValue(ii));
                    Console.Write(",");
                }
                // Separate each array with newline:
                Console.WriteLine();
            }
        }

Thank you all. This does solve the problem. I'm doing some signal processing and need a way to write the data arrays to csv file so I can plot a graph and analyse the results. The above solution is only going to be used for debugging and developing, so even though it may not be the most elegant method, it will save me having to manually write a for loop with each array in it, every time I want to output some data. If you have any suggestions for a better solution I would still appreciate hearing them, otherwise thanks for you help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.