C#我有三个按钮,每个按钮具有相同的循环但是不同的字符串可以将它缩短为一个循环吗?

时间:2017-01-21 23:43:21

标签: c# refactoring

我有三个按钮,每个按钮具有相同的循环但不同的字符串可以缩短为一个循环,所以我不必重复使用循环吗?

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();
}

3 个答案:

答案 0 :(得分:1)

只需创建一个方法并使用适当的参数调用它,如下所示:

var myEmitter = require('./my-emitter.js'); //Get your emitter from the module
myEmitter.on('img-op-complete', function(data){
   console.log(data); //You'll get your data here after op is done
})

注意:代码未经过测试。

答案 1 :(得分:1)

将重复的代码重构为

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();    
}

for循环只是构造一个带空格的字符串,可以用String.Join

替换

在事件处理程序中调用方法。

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);    
}

答案 2 :(得分:0)

我相信通过向字符串类添加扩展方法,您可以澄清代码然后进行一些基本的重构。这是一种方法。

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();
        }