mercredi 24 mars 2021

Index was out of range error when trying to use nested for loops and programmatic controls

I'm trying to create programmatic controls that pull down data from my database. However I get an index was out of range error with my nested for loops and if statements.

I'm trying to create "x" amount of car modifications based upon how many are assigned to the selected car.

I have put the controls into lists, as once this is working, I'm intending for the previous modifications to clear once another CarID has been selected in a combo box

Any help will be kindly appreciated. Thank you.

Global lists and objects at the top of form:

    DBConnect objConnect = new DBConnect();
    OptionalExtras objExtras = new OptionalExtras();
    CarConfigGroup objGroup = new CarConfigGroup();
    CarConfiguration objConfig;

    int iSelectedCarID = 0;
    List<Label> lblModTitle = new List<Label>();
    List<TextBox> txtModName = new List<TextBox>();
    List<TextBox> txtModPrice = new List<TextBox>();
    List<Label> lblCurrency = new List<Label>();

This is the section where I'm getting the error

for (int iCount = 0; iCount < objGroup.CarConfigurations[iSelectedCarID].CarMods.Count; iCount++) <-Error occurs on this line
        {
                if (objGroup.CarConfigurations[iSelectedCarID].CarMods[iCount].SelectedOptionID != 0)
                {
                    for (int iOption = 0; iOption < objGroup.CarConfigurations[iSelectedCarID].CarMods[iCount].ModOptions.Count; iOption++)
                    {
                        if (objGroup.CarConfigurations[iSelectedCarID].CarMods[iCount].SelectedOptionID == objGroup.CarConfigurations[iSelectedCarID].CarMods[iCount].ModOptions[iOption].OptID)
                        {

                        for (int iPriceCount = 0; iPriceCount < objGroup.CarConfigurations[iSelectedCarID].CarMods[iCount].ModOptions.Count; iPriceCount++)
                        {
                            lblModTitle.Add(new Label()
                            {
                                Name = "lblModTitle" + iCount.ToString(),

                                Location = new System.Drawing.Point(125 + (iRowCount * 275), 247 + (iColumnCount * 100)),
                                AutoSize = true,
                                Font = lblModPlaceholder.Font,
                                Text = "Modification " + (iCount + 1)
                            });

                            txtModName.Add(new TextBox()
                            {
                                Name = "txtMod" + iCount.ToString(),

                                Location = new System.Drawing.Point(128 + (iRowCount * 275), 274 + (iColumnCount * 100)),
                                Width = txtModPlaceholder.Width,
                                Font = txtModPlaceholder.Font,
                                Text = objConfig.CarMods[iCount].ModDisplayName
                            });

                            txtModPrice.Add(new TextBox()
                            {
                                Name = "txtModPrice" + iCount.ToString(),

                                Location = new System.Drawing.Point(158 + (iRowCount * 275), 300 + (iColumnCount * 100)),
                                Width = txtModPricePlaceholder.Width,
                                Font = txtModPricePlaceholder.Font,
                                Text = objConfig.CarMods[iCount].ModOptions[iPriceCount].OptPrice.ToString()
                            });

                            lblCurrency.Add(new Label()
                            {
                                Name = "lblModPrice" + iCount.ToString(),

                                Location = new System.Drawing.Point(139 + (iRowCount * 275), 303 + (iColumnCount * 100)),
                                AutoSize = true,
                                Font = lblModPricePlaceholder.Font,
                                Text = "£"
                            });

                            Controls.Add(lblModTitle[iTotalItems]);
                            Controls.Add(txtModName[iTotalItems]);
                            Controls.Add(txtModPrice[iTotalItems]);
                            Controls.Add(lblCurrency[iTotalItems]);

                            iRowCount++;

                            if ((iCount + 1) % iItemsPerRow == 0)
                            {
                                iColumnCount++;
                                iRowCount = 0;
                            }
                        }

                        }
                    }
                }
        }

Relevant class data:

class CarConfigGroup
{
    List<CarConfiguration> _CarConfigurations = new List<CarConfiguration>();

    public List<CarConfiguration> CarConfigurations { get => _CarConfigurations; set => _CarConfigurations = value; }
}


class CarConfiguration
{
    int _CfgID = 0;
    int _UserID;
    int _CarID;
    string _CarName;
    int _Status;
    int _Discount;
    List<CarModification> _CarMods = new List<CarModification>();


    public int CfgID { get => _CfgID; set => _CfgID = value; }
    public int UserID { get => _UserID; set => _UserID = value; }
    public int CarID { get => _CarID; set => _CarID = value; }
    public string CarName { get => _CarName; set => _CarName = value; }
    public int Status { get => _Status; set => _Status = value; }
    public int Discount { get => _Discount; set => _Discount = value; }
    public List<CarModification> CarMods { get => _CarMods; set => _CarMods = value; }
   }


class CarModification
{
    int _ModID;
    string _ModDisplayName;
    List<CarModOption> _ModOptions = new List<CarModOption>();
    int _SelectedOptionID = 0;

    public int ModID { get => _ModID; set => _ModID = value; }
    public string ModDisplayName { get => _ModDisplayName; set => _ModDisplayName = value; }
    public List<CarModOption> ModOptions { get => _ModOptions; set => _ModOptions = value; }
    public int SelectedOptionID { get => _SelectedOptionID; set => _SelectedOptionID = value; }
    }

 class CarModOption
 {
    int _OptID;
    string _OptDisplayText;
    int _OptPrice;

    public int OptID { get => _OptID; set => _OptID = value; }
    public string OptDisplayText { get => _OptDisplayText; set => _OptDisplayText = value; }
    public int OptPrice { get => _OptPrice; set => _OptPrice = value; }
  }

Aucun commentaire:

Enregistrer un commentaire