Remove redundancies in code

177 views Asked by At

I'm still new at this so I will try to explain my problem the best I can. English is not my first language so I apologize if I use some terms incorrectly.

I have a 100 line code that is executed every time a button is pressed. My problem is, I have 20 buttons and they all contain the same code (they are only slightly different in means of grabbing info from different source). Is there any way to do this instead of copying the same code too many times.

Basically my code is this:

    private void button1_Click(object sender, EventArgs e)
    {
    //file data source url
    sourceUrl = ("www.myurl.com")

    //Grab data
    code
    code
    code

   //Store data
    code
    code
    code

   //Write data
    code
    code
    code
    }

Every button has the same code except for the "sourceUrl" part. If I want to add more buttons I have to copy>paste the whole code and my application is starting to get HUGE. Is there any way to shrink the code by only having the code once, and then calling an action or method every time the button is pressed. So instead of having 100 line code multiple time, I'll have one line code for each button and one 100 line code on the top that will be the source for that one line code.

Thanks in advance

4

There are 4 answers

1
dotNET On BEST ANSWER

That's what functions are for. Use this layout:

private void YourFunc(string sourceUrl)
{
    //Grab data
    code

    //Store data
    code

    //Write data
    code
}

Now your buttons' event handlers look like this:

private void button1_Click(object sender, EventArgs e)
{
    YourFunc("www.myurl.com");
}

private void button2_Click(object sender, EventArgs e)
{
    YourFunc("www.myurl2.com");
}
0
Suraj Singh On

Here we can use use CommandName property, by passing URL in every button's CommandName property and passing it as a parameter to your Common Method to fetch data , So you can create a single function and call it through you btn_Click event .

<asp:Button ID="button1" runat="server" Text="clickMe"   
                        CommandName="put your URL here" OnCommand="button1_Click" />
                        <%--OnClick="button1_Click" />--%>

See here we can pass your 'URL' in CommandName property ,few things to keep in mind , Here we are using OnCommand event rather than OnClick event of button , so that we can use 'CommandName' property here .1. OnCommand MSDN 2. Commandname MSDN

 // private void button1_Click(object sender, EventArgs e)
       private void button1_Click(object sender, CommandEventArgs e)
        {             
         string sourceUrl = Convert.tostring(e.CommandName) 
         // Call function to grab data pass URL as parameter.
          GrabDate (sourceUrl ) ;

So now we can get the URL value from button CommandName property .

3 . EventArgs Class

4 .CommandEventArgsClass

2
Steve On

Use the Tag property of your buttons to store the source url string and then set, for every button, the same event handler

private void buttonCommonHandler_Click(object sender, EventArgs e)
{
     Button b = sender as Button;
     CommonMethod(b.Tag.ToString());
}

private void CommonMethod(string sourceUrl)
{
   // Execute the common code here....
}

You could set the common handler and the Tag using the form designer window or you could do that dynamically mimicking the code prepared for you by the designer in the InitializeComponent call

button1.Click += buttonCommonHandler;
button1.Tag = "www.myurl.com";
button2.Click += buttonCommonHandler;
button2.Tag = "www.anotherurl.com";
0
SynerCoder On

Sure there is a way. Just make the whole function a function that takes the url as a string parameter. And then call that function from your code behind.

private void button1_Click(object sender, EventArgs e)
{
    //file data source url
    ProcessData("www.myurl.com");
}

private void ProcessData(string sourceUrl)
{
    //Grab data
    code
    code
    code

    //Store data
    code
    code
    code

    //Write data
    code
    code
    code
}