简体   繁体   中英

C# I have three buttons each with the same loop but different string can I shorten it to one loop?

I have three buttons each with the same loop but different string can I shorten it to one loop so I don't have to keep reusing the loop?

public static string VarOutput { get; set; } 

async private void btnCourse1_Click(object sender, RoutedEventArgs e)
{
    string VarOutput = "";
    string[] names = new string[3] { "COP3488C,", "UWP1,", "This course is mobile app development." };

    for (int i = 0; i < names.Length; i++)
    {
        VarOutput = VarOutput + names[i] + "  ";
    }
    txtBoxCourse.Text = VarOutput;
    var dialog = new MessageDialog(VarOutput);
    await dialog.ShowAsync();
}
async private void btnCourse2_Click(object sender, RoutedEventArgs e)
{
    string VarOutput = "";
    string[] names = new string[3] { "DOP3488B,", "UWC1,", "This course is Cloud Computing." };

    for (int i = 0; i < names.Length; i++)
    {
        VarOutput = VarOutput + names[i] + "  ";
    }
    txtBoxCourse.Text = VarOutput;
    var dialog = new MessageDialog(VarOutput);
    await dialog.ShowAsync();
}

async private void btnCourse3_Click(object sender, RoutedEventArgs e)
{
    string VarOutput = "";
    string[] names = new string[3] { "BOP3589,", "UWP2,", "This course Computer Programming Java 1." };

    for (int i = 0; i < names.Length; i++)
    {
        VarOutput = VarOutput + names[i] + "  ";
    }
    txtBoxCourse.Text = VarOutput;
    var dialog = new MessageDialog(VarOutput);
    await dialog.ShowAsync();
}

Just create a method and call it with the proper parameters, like this:

async private void btnCourse1_Click(object sender, RoutedEventArgs e)
{
    string[] names = new string[3] { "COP3488C,", "UWP1,", "This course is mobile app development." };
    await WorkerAsync(names);
}
async private void btnCourse2_Click(object sender, RoutedEventArgs e)
{
    string[] names = new string[3] { "DOP3488B,", "UWC1,", "This course is Cloud Computing." };
    await WorkerAsync(names);
}

async private void btnCourse3_Click(object sender, RoutedEventArgs e)
{

    string[] names = new string[3] { "BOP3589,", "UWP2,", "This course Computer Programming Java 1." };
    await WorkerAsync(names);

}
private async Task WorkerAsync(string[] names)
{
        string VarOutput = "";

        for (int i = 0; i < names.Length; i++)
        {
            VarOutput = VarOutput + names[i] + "  ";
        }
        txtBoxCourse.Text = VarOutput;
        var dialog = new MessageDialog(VarOutput);
        await dialog.ShowAsync();
}

Note: Code is not tested.

Refactor the repeated code to

private async Task displayCourseInfo(string[] names) {
    //Replaced for loop with this line;
    var message = String.Join(" ", names);

    txtBoxCourse.Text = message;
    var dialog = new MessageDialog(message);
    await dialog.ShowAsync();    
}

The for loop is simply constructing a string with spaces which can be replaced with a String.Join

Call the method in the event handlers.

private async void btnCourse1_Click(object sender, RoutedEventArgs e) {
    var names = new []{ "COP3488C,", "UWP1,", "This course is mobile app development." };
    await displayCourseInfo(names);
}

private async void btnCourse2_Click(object sender, RoutedEventArgs e) {
    var names = new []{ "DOP3488B,", "UWC1,", "This course is Cloud Computing." };
    await displayCourseInfo(names);
}

private async void btnCourse3_Click(object sender, RoutedEventArgs e) {    
    var names = new []{ "BOP3589,", "UWP2,", "This course Computer Programming Java 1." };
    await displayCourseInfo(names);    
}

I believe by adding an extension method to the string class you may clarify the code and then doing some basic refactoring. This is one way to do it.

namespace ConsoleApplication2
{
    public static class myExtensionMethods
    {
        public static string GetSubs(this string[] input)
        {
            string value = "";
            input.Select(sub => value += $"{sub} ");
            return value;
        }
    }
    class Program
    {        

        async private void btnCourse1_Click(object sender, RoutedEventArgs e)
        {
            await ShowDialogAsync(new string[] { "COP3488C,", "UWP1,", "This course is mobile app development." });
        }

        async private void btnCourse2_Click(object sender, RoutedEventArgs e)
        {
            await ShowDialogAsync(new string[3] { "DOP3488B,", "UWC1,", "This course is Cloud Computing." });
        }

        async private void btnCourse3_Click(object sender, RoutedEventArgs e)
        {
            await ShowDialogAsync(new string[3] { "BOP3589,", "UWP2,", "This course Computer Programming Java 1." });
        }

        private async Task ShowDialogAsync(string [] myStringArray)
        {
            string VarOutput = myStringArray.GetSubs();
            txtBoxCourse.Text = VarOutput;
            var dialog = new MessageDialog(VarOutput);
            await dialog.ShowAsync();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM