Solved

how can i reduce this coding in C# .net ??

Posted on 2011-02-25
10
249 Views
1 Endorsement
Last Modified: 2012-06-21
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
Comment
Question by:Parth48
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
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
 
LVL 11

Expert Comment

by:Sudhakar Pulivarthi
Comment Utility
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
 
LVL 11

Expert Comment

by:Sudhakar Pulivarthi
Comment Utility
Hi,

DataTable dtable1 = new DataTable(); // This statement is not required as it gets datatable from objGeneral.Table in the try catch.
0
 
LVL 11

Expert Comment

by:Sudhakar Pulivarthi
Comment Utility
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
 
LVL 10

Expert Comment

by:Asim Nazir
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 10

Expert Comment

by:John Claes
Comment Utility
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
 
LVL 11

Accepted Solution

by:
bansidhar earned 500 total points
Comment Utility
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
 
LVL 11

Expert Comment

by:bansidhar
Comment Utility
poor_beggar, you posted the same solution while i was typing. sry i didn't see that
0
 

Author Comment

by:Parth48
Comment Utility
Thanks very much ....
0
 
LVL 10

Expert Comment

by:John Claes
Comment Utility
@bansidhar: No problem

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

regards
poor beggar
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now