Advertisement

07.11.2008 at 03:20PM PDT, ID: 23558834
[x]
Attachment Details

How would I build a class and eliminate the repeated code? C#

Asked by RalphHxyz in C# Programming Language, Microsoft Visual C#.Net, Microsoft Visual Studio Express

Tags: Microsoft, ASP.NET C#

This works but is ugly. I have just used my first class to do repeated operaations and definately can see the advantage but I cannot figure out the sytntax I need to use to build the class.

This works:

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            TableBuilder objTableBuilder = new TableBuilder();
            Label1.Text = objTableBuilder.buildTable();
            Label1.Visible = true;
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        TableBuilder objTableBuilder = new TableBuilder();

        //addTables = objTableBuilder.buildTable();     //Tried to use addTables class instead of repeating code

        string newLabel = "Label" + holder.c + ".Text";

        newLabel = objTableBuilder.buildTable();

        Label2.Text = objTableBuilder.buildTable();

        Button2.Visible = false;

        Label2.Visible = true;

        Button3.Visible = true;
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        string newLabel = "Label" + holder.c + ".Text";

        TableBuilder objTableBuilder = new TableBuilder();

        newLabel = objTableBuilder.buildTable();

        Label3.Text = objTableBuilder.buildTable();

        Button3.Visible = false;

        Label3.Visible = true;

        Button4.Visible = true;
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        string newLabel = "Label" + holder.c + ".Text";

        TableBuilder objTableBuilder = new TableBuilder();

        newLabel = objTableBuilder.buildTable();

        Label4.Text = objTableBuilder.buildTable();

        Button4.Visible = false;

        Label4.Visible = true;


    }
}
I need to dynamically name Buttons and Labels and add and change their properties.

All of the simple methods I have dreamed up fail.

Thanks for the help.

RalphStart Free Trial
 
Loading Advertisement...
 
[+][-]07.11.2008 at 03:36PM PDT, ID: 21986590

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.12.2008 at 01:49AM PDT, ID: 21988302

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.12.2008 at 02:04AM PDT, ID: 21988348

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: C# Programming Language, Microsoft Visual C#.Net, Microsoft Visual Studio Express
Tags: Microsoft, ASP.NET C#
Sign Up Now!
Solution Provided By: margajet24
Participating Experts: 2
Solution Grade: A
 
 
[+][-]07.14.2008 at 08:26AM PDT, ID: 21998922

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.14.2008 at 09:23AM PDT, ID: 21999538

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.14.2008 at 10:03AM PDT, ID: 21999909

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.15.2008 at 07:17AM PDT, ID: 22007218

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.15.2008 at 12:27PM PDT, ID: 22010155

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.18.2008 at 10:19AM PDT, ID: 22037947

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628