none
Please critique: Passing an int array argument into a method.

    Question

  • I think I just may have done this pretty good. But I would love to read what anyone would suggest.

    What this does is pass an int array as an argument from a method call and then return the sum of the elements in that array as an int total. My question is really, I see that int, double, string can be returned, but how about objects? Would it be possible to return an object like an array?

    using System;

    using System.Collections.Generic;

    using System.Text;

    namespace ArrayThis

    {

    class ParamArrayWithReturn

    {

    static int[] toBSummed = { 50, 1000, 175 };

    static int summedAll = 0;

    public static void Main()

    {

    SumThisArray(toBSummed);

    Console.WriteLine(summedAll);

    }

    //The method that will accept an int array and then return the sum of all elements inside it

    public static int SumThisArray(int[] B)

    {

    int[] copyOfBSummed = new int[3];

    copyOfBSummed = B;

    foreach(int indx in copyOfBSummed)

    summedAll += indx;

    return summedAll;

    }

     

    }

    }

    Thank you for teaching me how to use the foreach. I think it worked quite well here.

    Sunday, November 12, 2006 4:22 PM

Answers

  • well I would say that its pretty pointless passing the object array to another method since the object array is declared globally any way, meaning that any method can access it since its in the scope of the entire class.

    A better way of doing the calculation in your SumThisArray method:

    public static int SumThisArray()

    {

    int theSum = 0;

    foreach(int indx in toBSummed)

    {

    theSum += indx;

    }

    return theSum;

    }

     

    this will go through each element, as you had, of B and add sum up the values locally and return it back to the caller. Instead of having to copy the parameter into another array then going through that - its a bit inefficient.

    so....



    public static void Main()
    {
    Console.WriteLine(SumThisArray().ToString());
    }
    //The method that will accept an int array and then return the sum of all elements inside it

    public static int SumThisArray()
    {

    int theSum = 0;
    foreach(int indx in toBSummed)
    {
    theSum += indx;
    }
    return theSum;

    }

    }

     

    I hope this makes sense and hope that you find this in a good sense to better improve you! :-)

     

    in regards to passing objects in generally, you can do so but you need to know what the object contains otherwise if you try to say, add the values up in an object but the object array only contains strings - it won't work. in .NET 2.0 you can use a Generic List (List<int> in this case) to hold a collection of integers and go through each element and add the values together, pretty much the same as you are doing now.

     

    you could use your current approach to pass the array into the method and do sum ups....something like this for example:

     



    public static void Main()
    {
    int[] theCollection1 = new int[3] = {1, 2, 3};
    Console.WriteLine(SumThisArray(theCollection1).ToString());

    int[] theCollection2 = new int[3] = {4, 5, 6};
    Console.WriteLine(SumThisArray(theCollection2).ToString());
    }
    //The method that will accept an int array and then return the sum of all elements inside it

    public static int SumThisArray(int[] theArray)
    {

    int theSum = 0;
    foreach(int indx in theArray)
    {
    theSum += indx;
    }
    return theSum;

    }

    }

     

     

    so basically you pass the int[] array into this method which sums up the items in the collection, and calling it you just pass the int[] array which will take care of the calculations for you

    Sunday, November 12, 2006 4:31 PM
    Moderator