Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 265
  • Last Modified:

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;
        }

Open in new window


how can i reduce the above coding using for loop ??

give me some suggestions ....
1
Parth48
Asked:
Parth48
  • 3
  • 2
  • 2
  • +3
1 Solution
 
DhaestCommented:
Is Que1Options also a control on the form or what is that ?
public DataTable GetQuestionOptions()
        {
            DataTable dtable1 = new DataTable();
            try
            {
                dtable1 = objGeneral.Table(objAnswer.get_Option().ToString());
                String[] arystr = new String[12];
//                int k = 0;
                for (int i = 1; i<=3;i++)
                {
//                    arystr[k] = this.Controls["Que" + i.ToString() + "Options"][i - 1];
//                    k++;
                    this.Controls["lblOption" + i.ToString() + "a"].Text = dtable1.Rows[i].ItemArray[0].ToString();
                    this.Controls["lblOption" + i.ToString() + "b"].Text = dtable1.Rows[i].ItemArray[1].ToString();
                    this.Controls["lblOption" + i.ToString() + "c"].Text = dtable1.Rows[i].ItemArray[2].ToString();
                    this.Controls["lblOption" + i.ToString() + "d"].Text = dtable1.Rows[i].ItemArray[3].ToString();
                }

                //creating list control for question options
                

                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
            {

            }

            return dtable1;
        }

Open in new window

0
 
Sudhakar PulivarthiProject Lead - EngineeringCommented:
Hi,

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;
        }

Open in new window

0
 
Sudhakar PulivarthiProject Lead - EngineeringCommented:
Hi,

DataTable dtable1 = new DataTable(); // This statement is not required as it gets datatable from objGeneral.Table in the try catch.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Sudhakar PulivarthiProject Lead - EngineeringCommented:
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.
0
 
Asim NazirCommented:
Hi,

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();
            }

Open in new window


There is no need of creating String arrays at least!
Let me know if you need further help.
0
 
John ClaesSenior .Net Consultant & Technical AnalistCommented:
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.

 
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;

	}
}

Open in new window


regards
poor beggar
0
 
bansidharCommented:
not checked this in .net
try this
public DataTable GetQuestionOptions()
        {
            dtable1 = new DataTable();
            try
            {
                dtable1 = objGeneral.Table(objAnswer.get_Option().ToString());
                String[] arystr = new String[12];
                for (int i = 0; i < 4; i++){
                    for (int j =0; j< 4; j++){
                        this.Controls[String.Format("lblOption{0}{1}",i+1, (char)(97+j))].Text=dtable1.Rows[i].ItemArray[j].ToString();
                        arystr[(i*4)+j]=dtable1.Rows[i].ItemArray[j].ToString();
                    }
                }
               
                General.QuestionOptions = arystr;
            }
            catch (Exception ex)
            {

            }
            finally 
            {

            }
            return dtable1;
        }

Open in new window

0
 
bansidharCommented:
poor_beggar, you posted the same solution while i was typing. sry i didn't see that
0
 
Parth48Author Commented:
Thanks very much ....
0
 
John ClaesSenior .Net Consultant & Technical AnalistCommented:
@bansidhar: No problem

I Only had to laugh with the Simularities in the Code ;-)

regards
poor beggar
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now