The following code worked for me but I wanted to re-do it to learn how to make a Object Array. The first set of code is the working code, the 2nd is my attempt at it. Thanks for checking this out:
public void AddPrices()
{
string[] cb = new string[11];
int i = 0;
if (tabControl1.SelectedIndex == 0)
{
foreach (Control c in tabControl1.SelectedTab.Controls)
{
if (c is ComboBox)
{
cb[i] = "[SLS-02] = '" + c.Text + "'";
i++;
}
}
}
DataRow[] rcb1 = dt.Select(cb[0]);
DataRow[] rcb2 = dt.Select(cb[1]);
DataRow[] rcb3 = dt.Select(cb[2]);
DataRow[] rcb4 = dt.Select(cb[3]);
DataRow[] rcb5 = dt.Select(cb[4]);
DataRow[] rcb6 = dt.Select(cb[5]);
DataRow[] rcb7 = dt.Select(cb[6]);
DataRow[] rcb8 = dt.Select(cb[7]);
DataRow[] rcb9 = dt.Select(cb[8]);
DataRow[] rcb10 = dt.Select(cb[9]);
DataRow[] rcb11 = dt.Select(cb[10]);
labelPrice.Text = string.Empty;
decimal sumprice = 0;
decimal[] cbp = new decimal[11];
for (int k = 0; k <= 10; k++)
{
cbp[k] = 0;
}
foreach (DataRow row in rcb1)
{
labelPrice.Text += row[0].ToString() + " " + row[1].ToString() + "\n";
cbp[0] = Convert.ToDecimal(row[1].ToString());
sumprice += cbp[0];
}
foreach (DataRow row in rcb2)
{
labelPrice.Text += row[0].ToString() + " " + row[1].ToString() + "\n";
cbp[1] = Convert.ToDecimal(row[1].ToString());
sumprice += cbp[1];
}
foreach (DataRow row in rcb3)
{
labelPrice.Text += row[0].ToString() + " " + row[1].ToString() + "\n";
cbp[2] = Convert.ToDecimal(row[1].ToString());
sumprice += cbp[2];
}
foreach (DataRow row in rcb4)
{
labelPrice.Text += row[0].ToString() + " " + row[1].ToString() + "\n";
cbp[3] = Convert.ToDecimal(row[1].ToString());
sumprice += cbp[3];
}
foreach (DataRow row in rcb5)
{
labelPrice.Text += row[0].ToString() + " " + row[1].ToString() + "\n";
cbp[4] = Convert.ToDecimal(row[1].ToString());
sumprice += cbp[4];
}
foreach (DataRow row in rcb6)
{
labelPrice.Text += row[0].ToString() + " " + row[1].ToString() + "\n";
cbp[5] = Convert.ToDecimal(row[1].ToString());
sumprice += cbp[5];
}
foreach (DataRow row in rcb7)
{
labelPrice.Text += row[0].ToString() + " " + row[1].ToString() + "\n";
cbp[6] = Convert.ToDecimal(row[1].ToString());
sumprice += cbp[6];
}
foreach (DataRow row in rcb8)
{
labelPrice.Text += row[0].ToString() + " " + row[1].ToString() + "\n";
cbp[7] = Convert.ToDecimal(row[1].ToString());
sumprice += cbp[7];
}
foreach (DataRow row in rcb9)
{
labelPrice.Text += row[0].ToString() + " " + row[1].ToString() + "\n";
cbp[8] = Convert.ToDecimal(row[1].ToString());
sumprice += cbp[8];
}
foreach (DataRow row in rcb10)
{
labelPrice.Text += row[0].ToString() + " " + row[1].ToString() + "\n";
cbp[9] = Convert.ToDecimal(row[1].ToString());
sumprice += cbp[9];
}
foreach (DataRow row in rcb11)
{
labelPrice.Text += row[0].ToString() + " " + row[1].ToString() + "\n";
cbp[10] = Convert.ToDecimal(row[1].ToString());
sumprice += cbp[10];
}
labelPrice.Text += "\nTotal: $" + sumprice;
}
My attempt (it compiles fine but when I do a testrun stacktrace points the error at "string[] cb = new string[11];":
public void AddPrices()
{
string[] cb = new string[11];
for (int h = 0; h <= 10; h++)
cb[h] = string.Empty;
int i = 0;
if (tabControl1.SelectedIndex == 0)
{
foreach (Control c in tabControl1.SelectedTab.Controls)
{
if (c is ComboBox)
{
cb[i] = "[SLS-02] = '" + c.Text + "'";
i++;
}
}
}
Object[][] rcb = new object[11][];
for (int j = 0; j <= 10; j++)
{
rcb[j] = new DataRow[11];
rcb[j] = dt.Select(cb[j]);
}
labelPrice.Text = string.Empty;
decimal sumprice = 0;
decimal[] cbp = new decimal[11];
for (int k = 0; k <= 10; k++)
cbp[k] = 0;
int l = 0;
foreach (Object[] o in rcb)
{
foreach (DataRow[] row in rcb)
{
labelPrice.Text += row[0].ToString() + " " + row[1].ToString() + "\n";
cbp[l] = Convert.ToDecimal(row[1].ToString());
sumprice += cbp[l];
l++;
}
}
labelPrice.Text += "\nTotal: $" + sumprice;
}
}