Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Basics (Retired) Perfect Variable Scope

Scoping with DRY standard?

So I'm getting the compiler error output does not exist in the current context, so I just put the line Console.WriteLine(output); in each part of the if/else and it worked. Is that what you're supposed to do, because that doesn't seem very DRY methodology, unless they're doing it because it's a basics section?

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {            
            string input = Console.ReadLine();

            if (input == "quit")
            {
                string output = "Goodbye.";
            }
            else
            {
                string output = "You entered " + input + ".";
            }

            Console.WriteLine(output);
        }
    }
}

1 Answer

Mikkel Rasmussen
Mikkel Rasmussen
31,772 Points

You could shorten the code a bit by doing it this way but is that more DRY than yours am I not sure about.

DRY will make more sense when you start to learn about functions :)

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {            
            string input = Console.ReadLine();
            string output = "You entered " + input + ".";

            if (input == "quit")
            {
                output = "Goodbye.";
            }

            Console.WriteLine(output);
        }
    }
}