Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-02-25
10
Medium Priority
?
257 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 34978282
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
ID: 34978306
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
ID: 34978321
Hi,

DataTable dtable1 = new DataTable(); // This statement is not required as it gets datatable from objGeneral.Table in the try catch.
0
PowerShell Core for Advanced Linux Administrators

Understand advanced principals around Powershell Core with a focus on the Linux Administrator.  This course covers how to administer numerous environments across multiple platforms including Linux, Azure, AWS, and Google Cloud from a single shell instance.

 
LVL 11

Expert Comment

by:Sudhakar Pulivarthi
ID: 34978341
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
ID: 34978344
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
 
LVL 10

Expert Comment

by:John Claes
ID: 34978429
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 2000 total points
ID: 34978565
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
ID: 34978572
poor_beggar, you posted the same solution while i was typing. sry i didn't see that
0
 

Author Comment

by:Parth48
ID: 34994577
Thanks very much ....
0
 
LVL 10

Expert Comment

by:John Claes
ID: 34995082
@bansidhar: No problem

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

regards
poor beggar
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

715 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