Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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