Parth48
asked on
how can i reduce this coding in C# .net ??
public DataTable GetQuestionOptions()
{
dtable1 = new DataTable();
try
{
dtable1 = objGeneral.Table(objAnswer.get_Option().ToString());
// old code created on 2nd February 2011
//Getting Question 1 Options
string[] Que1Options = new string[4];
for (int i = 0; i < 4; i++)
{
Que1Options[i] = dtable1.Rows[0].ItemArray[i].ToString();;
}
lblOption1a.Text = Que1Options[0];
lblOption1b.Text = Que1Options[1];
lblOption1c.Text = Que1Options[2];
lblOption1d.Text = Que1Options[3];
//Getting Question 2 Options
string[] Que2Options = new string[4];
for (int i = 0; i < 4; i++)
{
Que2Options[i] = dtable1.Rows[1].ItemArray[i].ToString(); ;
}
lblOption2a.Text = Que2Options[0];
lblOption2b.Text = Que2Options[1];
lblOption2c.Text = Que2Options[2];
lblOption2d.Text = Que2Options[3];
//Getting Question 3 Options
string[] Que3Options = new string[4];
for (int i = 0; i < 4; i++)
{
Que3Options[i] = dtable1.Rows[2].ItemArray[i].ToString(); ;
}
lblOption3a.Text = Que3Options[0];
lblOption3b.Text = Que3Options[1];
lblOption3c.Text = Que3Options[2];
lblOption3d.Text = Que3Options[3];
//creating list control for question options
String[] arystr = new String[12];
arystr[0] = Que1Options[0];
arystr[1] = Que1Options[1];
arystr[2] = Que1Options[2];
arystr[3] = Que1Options[3];
arystr[4] = Que2Options[0];
arystr[5] = Que2Options[1];
arystr[6] = Que2Options[2];
arystr[7] = Que2Options[3];
arystr[8] = Que3Options[0];
arystr[9] = Que3Options[1];
arystr[10] = Que3Options[2];
arystr[11] = Que3Options[3];
General.QuestionOptions = arystr;
}
catch (Exception ex)
{
}
finally
{
}
/* dynamically create lable text on 7th february
Label[,] AryLbl = new Label[3, 5];
AryLbl[0,1] = lblOption1a;
AryLbl[0,2] = lblOption1b;
AryLbl[0,3] = lblOption1c;
AryLbl[0,4] = lblOption1d;
for (int i = 1; i < 5; i++){
for (int j = 0; j < 1; j++)
{
((Label)AryLbl[j, i]).Text = dtable1.Rows[j].ItemArray[i].ToString();
}
}
*/
return dtable1;
}
how can i reduce the above coding using for loop ??
give me some suggestions ....
Hi,
I have optimized with it.
I have optimized with it.
public DataTable GetQuestionOptions()
{
DataTable dtable1 = null;
//creating list control for question options
try
{
General.QuestionOptions = new String[12];
dtable1 = objGeneral.Table(objAnswer.get_Option().ToString());
//Getting Question 1 Options
General.QuestionOptions[0] = lblOption1a.Text = Convert.ToString(dtable1.Rows[0].ItemArray[0]);
General.QuestionOptions[1] = lblOption1b.Text = Convert.ToString(dtable1.Rows[0].ItemArray[1]);
General.QuestionOptions[2] = lblOption1c.Text = Convert.ToString(dtable1.Rows[0].ItemArray[2]);
General.QuestionOptions[3] = lblOption1d.Text = Convert.ToString(dtable1.Rows[0].ItemArray[3]);
//Getting Question 2 Options
General.QuestionOptions[4] = lblOption2a.Text = Convert.ToString(dtable1.Rows[1].ItemArray[0]);
General.QuestionOptions[5] = lblOption2b.Text = Convert.ToString(dtable1.Rows[1].ItemArray[1]);
General.QuestionOptions[6] = lblOption2c.Text = Convert.ToString(dtable1.Rows[1].ItemArray[2]);
General.QuestionOptions[7] = lblOption2d.Text = Convert.ToString(dtable1.Rows[1].ItemArray[3]);
//Getting Question 3 Options
General.QuestionOptions[8] = lblOption3a.Text = Convert.ToString(dtable1.Rows[2].ItemArray[0]);
General.QuestionOptions[9] = lblOption3b.Text = Convert.ToString(dtable1.Rows[2].ItemArray[1]);
General.QuestionOptions[10]= lblOption3c.Text = Convert.ToString(dtable1.Rows[2].ItemArray[2]);
General.QuestionOptions[11]= lblOption3d.Text = Convert.ToString(dtable1.Rows[2].ItemArray[3]);
}
catch (Exception ex)
{
}
finally
{
}
/* dynamically create lable text on 7th february
Label[,] AryLbl = new Label[3, 5];
AryLbl[0,1] = lblOption1a;
AryLbl[0,2] = lblOption1b;
AryLbl[0,3] = lblOption1c;
AryLbl[0,4] = lblOption1d;
for (int i = 1; i < 5; i++){
for (int j = 0; j < 1; j++)
{
((Label)AryLbl[j, i]).Text = dtable1.Rows[j].ItemArray[i].ToString();
}
}
*/
return dtable1;
}
Hi,
DataTable dtable1 = new DataTable(); // This statement is not required as it gets datatable from objGeneral.Table in the try catch.
DataTable dtable1 = new DataTable(); // This statement is not required as it gets datatable from objGeneral.Table in the try catch.
Hi,
string a= null;
string b = Convert.ToString(a); // value is null; no error
string b = a.ToString(); // error Null Refernce exception
Use Convert.ToString() instead when ever u do convertion.
string a= null;
string b = Convert.ToString(a); // value is null; no error
string b = a.ToString(); // error Null Refernce exception
Use Convert.ToString() instead when ever u do convertion.
Hi,
Do something like this:
There is no need of creating String arrays at least!
Let me know if you need further help.
Do something like this:
for (int i = 0; i < 4; i++)
{
Control[] ctrl = this.Controls.Find("lblOption" + Convert.ToString(i+1) + "a", true);
ctrl[0].Text = dtable1.Rows[i].ItemArray[i].ToString();
}
There is no need of creating String arrays at least!
Let me know if you need further help.
I would make a nested For loop
I needed to use the rowcounter+1 to get the right numbering of the Question options
I needed to convert.ToChar because you are working with letters at the end of the optionxontrols.
regards
poor beggar
I needed to use the rowcounter+1 to get the right numbering of the Question options
I needed to convert.ToChar because you are working with letters at the end of the optionxontrols.
using System;
public class Class1
{
public Class1()
{
dtable1 = new DataTable();
try {
dtable1 = objGeneral.Table(objAnswer.get_Option().ToString());
for (int rowCounter = 0; i < dtable1.Rows.Count; rowCounter++)
{ // We loop through for every Row
for (int optionCounter = 0; optionCounter < 4; optionCounter++)
{
// Set the Item in de AryStr
arystr[rowCounter * 4 + optionCounter] =
// set the value for the OptionBox
this.Controls["lblOption" + (rowCounter+1).ToString() + Convert.ToChar(97 + optionCounter)].Text =
// Retrieve the Value
dtable1.Rows[rowCounter].ItemArray[optionCounter].ToString(); ;
}
}
General.QuestionOptions = arystr;
}
catch { }
finally{}
/* dynamically create lable text on 7th february
Label[,] AryLbl = new Label[3, 5];
AryLbl[0,1] = lblOption1a;
AryLbl[0,2] = lblOption1b;
AryLbl[0,3] = lblOption1c;
AryLbl[0,4] = lblOption1d;
for (int i = 1; i < 5; i++){
for (int j = 0; j < 1; j++)
{
((Label)AryLbl[j, i]).Text = dtable1.Rows[j].ItemArray[i].ToString();
}
}
*/
return dtable1;
}
}
regards
poor beggar
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
poor_beggar, you posted the same solution while i was typing. sry i didn't see that
ASKER
Thanks very much ....
@bansidhar: No problem
I Only had to laugh with the Simularities in the Code ;-)
regards
poor beggar
I Only had to laugh with the Simularities in the Code ;-)
regards
poor beggar
Open in new window