0

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();
}
2
  • can I shorten it to one loop yes Commented Jan 21, 2017 at 23:45
  • Better yet just replace it with string VarOutput = string.Join(" ", names); Commented Jan 22, 2017 at 0:09

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Your Worker method needs to be async and return Task. And the calls to it need to await it. (For that matter, it should be named WorkerAsync.)
@JoeWhite Just noticed the last line
1

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

6 Comments

Upvote for removing unnecessary loop, downvote for changing the final message
@CamiloTerevinto Settle down on those arbitrary downvotes for, at the worst, what amounts to trifles. If the concept is relayed then the answer is correct despite it not following strict style guidelines. A simple comment will suffice. Downvotes should be used for outright incorrect answers or site rules violations.
I like the .Join method. That is very nicely implemented.
@gmiley not sure where you saw the downvote, I just didn't vote at all. And the answer is incorrect, it changes the final message presented to the user
@CamiloTerevinto Well, on my answer to this question it was immediately downvoted at the same time you responded citing stylistic differences that had absolutely no bearing on the correctness of my answer. I decided to just delete my answer rather than deal with it, but then I saw you post a comment describing the same activity on this post, so I figured I would at the very least mention it. Maybe it was coincidence, I find though that is rarely the case.
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.