I want to use the code i have and turn it into a function that i can call in the main program. I have to do a similar function a second time and want to be able to call them into the main program instead of having one big long list of code.

Itried this . . but im really not sure what i should be doing . ..

namespace differenceofmeans
{
    public class TraceSet
    {
        public void Main()
        {
            TraceAverages;
            {
            Console.Write(avrg);
            }
        }

        public static TraceAverages (float avrg)
        {
            string file = System.IO.File.ReadAllText(@"C:\Users\40025634\Desktop\Project\TraceSet.csv");

            ArrayList rows = new ArrayList(file.Split('\n'));
            {

                foreach (string row in rows)
                {
                    string r = row.Remove(row.Length - 1, 1);

                    ArrayList rowVals = new ArrayList(r.Split(','));

                    float sum = 0;
                    float addedItems = 0;

                    foreach (string item in rowVals)
                    {
                        try
                        {
                            float num = float.Parse(item);
                            sum += num;
                            addedItems++;
                        }
                        catch (Exception)
                        {
                            //throw;
                        }
                    }

                    if (addedItems > 0)
                        {
                        float avrg = sum / addedItems;
                        ArrayList avrgvalues = new ArrayList();
                        avrgvalues.Add(avrg);
                        }

                        return avrg;

                        Console.ReadKey();

                    }
                }
            }
       }
}

Any help would be appriciated .

Instead of that code try this

namespace differenceofmeans
{
    public class TraceSet
    {
        public void Main()
        {
            float avrg = 5.6F;
            TraceAverages(avrg);
            Console.Write(avrg);
        }

        public static void  TraceAverages (float avrg)
        {
            string file = System.IO.File.ReadAllText(@"C:\Users\40025634\Desktop\Project\TraceSet.csv");

            ArrayList rows = new ArrayList(file.Split('\n'));
            {

                foreach (string row in rows)
                {
                    string r = row.Remove(row.Length - 1, 1);

                    ArrayList rowVals = new ArrayList(r.Split(','));

                    float sum = 0;
                    float addedItems = 0;

                    foreach (string item in rowVals)
                    {
                        try
                        {
                            float num = float.Parse(item);
                            sum += num;
                            addedItems++;
                        }
                        catch (Exception)
                        {
                            //throw;
                        }
                    }

                    if (addedItems > 0)
                        {
                        float avrg = sum / addedItems;
                        ArrayList avrgvalues = new ArrayList();
                        avrgvalues.Add(avrg);
                        }

                        return avrg;

                        Console.ReadKey();

                    }
                }
            }
       }
}

Sorry, instead of that try this a little bit modification.

namespace differenceofmeans
{
    public class TraceSet
    {
        public void Main()
        {
            float avrg = 5.6F;
            TraceAverages(avrg);
       }

        public static void TraceAverages (float avrg)
        {
            string file = System.IO.File.ReadAllText(@"C:\Users\40025634\Desktop\Project\TraceSet.csv");

            ArrayList rows = new ArrayList(file.Split('\n'));
            {

                foreach (string row in rows)
                {
                    string r = row.Remove(row.Length - 1, 1);

                    ArrayList rowVals = new ArrayList(r.Split(','));

                    float sum = 0;
                    float addedItems = 0;

                    foreach (string item in rowVals)
                    {
                        try
                        {
                            float num = float.Parse(item);
                            sum += num;
                            addedItems++;
                        }
                        catch (Exception)
                        {
                            //throw;
                        }
                    }

                    if (addedItems > 0)
                        {
                        float avrg = sum / addedItems;
                        ArrayList avrgvalues = new ArrayList();
                        avrgvalues.Add(avrg);
                        }

                        Console.Write(avrg);

                        Console.ReadKey();

                    }
                }
            }
       }
}

Instead of that code try this

namespace differenceofmeans
{
    public class TraceSet
    {
        public void Main()
        {
            float avrg = 5.6F;
            TraceAverages(avrg);
            Console.Write(avrg);
        }

        public static void  TraceAverages (float avrg)
        {
            string file = System.IO.File.ReadAllText(@"C:\Users\40025634\Desktop\Project\TraceSet.csv");

            ArrayList rows = new ArrayList(file.Split('\n'));
            {

                foreach (string row in rows)
                {
                    string r = row.Remove(row.Length - 1, 1);

                    ArrayList rowVals = new ArrayList(r.Split(','));

                    float sum = 0;
                    float addedItems = 0;

                    foreach (string item in rowVals)
                    {
                        try
                        {
                            float num = float.Parse(item);
                            sum += num;
                            addedItems++;
                        }
                        catch (Exception)
                        {
                            //throw;
                        }
                    }

                    if (addedItems > 0)
                        {
                        float avrg = sum / addedItems;
                        ArrayList avrgvalues = new ArrayList();
                        avrgvalues.Add(avrg);
                        }

                        return avrg;

                        Console.ReadKey();

                    }
                }
            }
       }
}

It didnt work. Surley the function cant be void because its ment to be returning the 'float avrg'. and it says that using:

float avrg = 5.6F ---- CHANGES THE VALUE OF AVRG IN THE REST OF THE PROGRAM.

im a bit confused.

namespace differenceofmeans
{
    public class TraceSet
    {
        public void Main()
        {
            float avrg = 0;
            TraceAverages(ref avrg);
        }
        public static void TraceAverages(ref float avrg)
        {
            string file = System.IO.File.ReadAllText(@"C:\Users\40025634\Desktop\Project\TraceSet.csv");

            ArrayList rows = new ArrayList(file.Split('\n'));
            {

                foreach (string row in rows)
                {
                    string r = row.Remove(row.Length - 1, 1);

                    ArrayList rowVals = new ArrayList(r.Split(','));

                    float sum = 0;
                    float addedItems = 0;

                    foreach (string item in rowVals)
                    {
                        try
                        {
                            float num = float.Parse(item);
                            sum += num;
                            addedItems++;
                        }
                        catch (Exception)
                        {
                            //throw;
                        }
                    }

                    if (addedItems > 0)
                    {
                        float avrg = sum / addedItems;
                        ArrayList avrgvalues = new ArrayList();
                        avrgvalues.Add(avrg);
                    }

                    Console.Write(avrg);

                    Console.ReadKey();

                }
            }
        }
    }
}

try this then, if u use ref keyword while passing the argument, then the resultant avrg value which is changing in the program will affect the argument passed originally.

namespace differenceofmeans
{
    public class TraceSet
    {
        public void Main()
        {
            float avrg = 0;
            TraceAverages(ref avrg);
        }
        public static void TraceAverages(ref float avrg)
        {
            string file = System.IO.File.ReadAllText(@"C:\Users\40025634\Desktop\Project\TraceSet.csv");

            ArrayList rows = new ArrayList(file.Split('\n'));
            {

                foreach (string row in rows)
                {
                    string r = row.Remove(row.Length - 1, 1);

                    ArrayList rowVals = new ArrayList(r.Split(','));

                    float sum = 0;
                    float addedItems = 0;

                    foreach (string item in rowVals)
                    {
                        try
                        {
                            float num = float.Parse(item);
                            sum += num;
                            addedItems++;
                        }
                        catch (Exception)
                        {
                            //throw;
                        }
                    }

                    if (addedItems > 0)
                    {
                        float avrg = sum / addedItems;
                        ArrayList avrgvalues = new ArrayList();
                        avrgvalues.Add(avrg);
                    }

                    Console.Write(avrg);

                    Console.ReadKey();

                }
            }
        }
    }
}

try this then, if u use ref keyword while passing the argument, then the resultant avrg value which is changing in the program will affect the argument passed originally.

This is what it is flagging up:

Error 1 A local variable named 'avrg' cannot be declared in this scope because it would give a different meaning to 'avrg', which is already used in a 'parent or current' scope to denote something else.

Edited 5 Years Ago by Mark_48: n/a

Hey, here i have given an example that's it, u shouldn't declare two variables with same name, Declare the passing argument variable with different name and Pass.

Hey, here i have given an example that's it, u shouldn't declare two variables with same name, Declare the passing argument variable with different name and Pass.

I dont understand if i change the variable avrg in the function it will not match the value in the main program. same if i cahange the variable avrg in the main program it wont match up with the variable in the function??

im sorry i just dont get it

This seems very similiar to your other question which is about the algorythm to use. Might I suggest resolving the algorythm and then coming back to this?

This seems very similiar to your other question which is about the algorythm to use. Might I suggest resolving the algorythm and then coming back to this?

yeh problem is i think i need to put it into functions to load both of the csv files. other wise it dosnt work.

yeh problem is i think i need to put it into functions to load both of the csv files. other wise it dosnt work.

Fair enough, the problem is that creating a function is not just a matter of creating a function per say. A function facilitates a (oh the irony) function that the system has to perform. Now without knowing what the function needs to facilitate it is extremely diffiuclt to write a function. Perhaps you could tell us what the function needs to do as opposed to the code that needs to go in it? This will dictate the return types and the input parameters :) Considering your question as to the average values, you could create one function that parses a csv file and returns the required results and use that function multiple times as opposed to one function that does it all.

Example:
Given the previous algorythm you might declare a function like this

public static List<float> GetRowAverages(string dataSet){

var dataSet = dataSet.Split('\n');

var floatList = new List<float>();

foreach (var row in dataSet){
	var split = row.Split(',');
	var rowTotal = split.Sum(columnVal => float.Parse(columnVal));
	floatList.Add(rowTotal / split.Length);
}

return floatList;
}

And then use it as follows

string fileData1 = System.IO.File.ReadAllText(@"C:\Users\40025634\Desktop\Project\SboxOutput.csv");
string fileData2 = System.IO.File.ReadAllText(@"C:\Users\40025634\Desktop\Project\AnotherOutput.csv");

var dataSetAverages1 = GetRowAverages(fileData1);
var dataSetAverages2 = GetRowAverages(fileData2);
var difList = dataSetAverages1.Select((t, i) => t - dataSetAverages2[i]).ToList();

Then you might wrap that in function called DoAllMyCVCalcs() etc etc

I hope you understand what I am getting at here. The point is to try and wrap "common" functionality, not to just wrap code.

This question has already been answered. Start a new discussion instead.