George Boulton
George Boulton

Reputation: 101

C# Divide the values of one list by the values of the other to produce a new list

I wish to divide the corresponding values of each list in order to produce a new list containing the divided results of each corresponding index in my two lists. Both my lists are 8 values long and are both int lists. I am currently creating two lists that are to be divided like so:

private void StartSchedule_Click(object sender, EventArgs e)
    {
        string ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\A2 Computing\C# Programming Project\TriHard.accdb";
        string SelectQuery = "SELECT Time.AthleteID, Athlete.AthleteName, Time.EventTime, Event.EventDistance FROM Event INNER JOIN (Athlete INNER JOIN [Time] ON Athlete.[AthleteID] = Time.[AthleteID]) ON Event.[EventID] = Time.[EventID];";
        OleDbConnection Connection = new OleDbConnection(ConnectionString);
        OleDbCommand Command = new OleDbCommand(SelectQuery, Connection);
        Command.Connection.Open();

        OleDbDataReader Reader = Command.ExecuteReader(CommandBehavior.CloseConnection);

        PaceCalculator pace = new PaceCalculator();

        List<int> Distancelist = new List<int>();
        List<int> Secondslist = new List<int>();
        List<int> Pacelist = new List<int>();
        while (Reader.Read())
        {
            pace = new PaceCalculator();
            pace.Distance = (int)Reader["EventDistance"];
            int DistanceInt = Convert.ToInt32(pace.Distance);
            Distancelist.Add(DistanceInt);

            pace = new PaceCalculator();
            pace.Time = (string)Reader["EventTime"]; //Reads in EventTime
            double Seconds = TimeSpan.Parse(pace.Time).TotalSeconds; //Converts the string into HH:MM:SS as a double
            int SecondsInt = Convert.ToInt32(Seconds); //Converts the double into an integer, returning the seconds in the total time
            Secondslist.Add(SecondsInt); //Adds the Seconds for each time to the list;

            //Need to fix this currently returns just 0
            var Pacelist2 = PaceCalc(Distancelist, Secondslist);
            listBox3.DisplayMember = "PaceInt";
            listBox3.DataSource = (Pacelist2);



        }


        listBox1.DisplayMember = "DistanceInt";
        listBox1.DataSource = Distancelist;

        listBox2.DisplayMember = "SecondsInt";
        listBox2.DataSource = Secondslist;

Here is the function I am calling which attempts to divide the lists, but doesn't seem to be working:

public List<int> PaceCalc(List<int> Dlist, List<int> Slist)
    {
        PaceCalculator pace = new PaceCalculator();
        List<int> Plist = new List<int>();

        pace = new PaceCalculator();
        for (int i = 0; i == Dlist.Count; i++)
        {
            int PaceInt = Dlist[i] / Slist[i];
            Plist.Add(PaceInt);
        }
        return Plist;
    }

I wish to display the outcomes of the division in listBox3. Am I dividing the lists correctly and how can I display it in the list box?

Upvotes: 1

Views: 2095

Answers (2)

Habib
Habib

Reputation: 223282

Couple of issues:

First you need to modify your check in for loop to i < Dlist.Count. Your current check i == Dlist.Count is wrong.

So your method would be:

public List<int> PaceCalc(List<int> Dlist, List<int> Slist)
{
    List<int> Plist = new List<int>();
    for (int i = 0; i < Dlist.Count; i++)
    {
        int PaceInt = Dlist[i] / Slist[i];
        Plist.Add(PaceInt);
    }
    return Plist;
}

(I have removed PaceCalculator pace = new PaceCalculator();, since you don't need that at all in your method)

Second. You don't have to specify DisplayMember for ListBox3

var Pacelist2 = PaceCalc(Distancelist, Secondslist);
listBox3.DataSource = Pacelist2;

Although, the second issue will not cause any error/exception, since DisplayMember will not be found , it will use the default ToString overload and you will get the number.

Upvotes: 3

itsme86
itsme86

Reputation: 19526

Your for loop is never executing because you're testing if i == Dlist.Count. It should be:

for (int i = 0;i < Dlist.Count; i++)

Alternatively, you could do this with LINQ:

public List<int> PaceCalc(List<int> Dlist, List<int> Slist)
{
    return Dlist.Zip(Slist, (a, b) => a / b).ToList();
}

Upvotes: 9

Related Questions