Rewriting a piece of code

codebeginnerg

Member
Joined
Oct 13, 2014
Messages
6
Programming Experience
Beginner
I've been having a hard to writing a simpler and/or condense way of rewriting this method. This is a method used in c#. Is there a way to simply or condense this code to make it more simpler to comprehend?

C#:
 private void prepareToPrint()
                {
              
                teams.Clear();// Clearing the list of teams
                 //Storing the teams and information in the List
                 foreach (DataRowView row in bindingSource1.List)
                   {
                  //Checking if information is true
                if ((combobox1.Text == "All Teams" || combobox1.Text == row["Home Team"].ToString()
                || combobox1Text == row["Away Team"].ToString())
                && (DateTime)row["Game_Date"] >= datebeg.Value &&
                (DateTime)row["Game_Date"] <= dateEnd2.Value)
                {
                short hScore = (short)row["Home Pts Scored"];
                short aScore = (short)row["Away Pts Scored"];
                string home = row["Home Team"].ToString();
                string away = row["Away Team"].ToString();
                DateTime date = (DateTime)row["Game_Date"];

                teamInfo teamStuff = new teamInfo(hScore, aScore, home, away, date);
                teams.Add(teamStuff);
                    }
               }

         
            }
 
You can split the if into two if statements.

// check team
if (combobox1.Text == "All Teams" ||
    combobox1.Text == row["Home Team"].ToString() ||
    combobox1Text == row["Away Team"].ToString())
{
    // check date
    if ((DateTime)row["Game_Date"] >= datebeg.Value &&
        (DateTime)row["Game_Date"] <= dateEnd2.Value)
    {
        // do your stuff
    }
}


You can also add a teamInfo constructor to the teamInfo class that takes the datarowview as a parameter and inside that constructor assign the values. In that case, you simply pass the row to the constructor.

teamInfo teamStuff = new teamInfo(row);
 
Back
Top Bottom