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 Final

Scoping error? ..and thinking about possible use of TryParse?

This code passes task 1 of 3:

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");
            //try
            //{
            var numInput = int.Parse(Console.ReadLine());
            //}
            //catch(FormatException error)
            //{
            //Console.Write(“You must enter a whole number.”);
            //}
            string print = "";
            for(int i = 0; i< numInput ; i++)
            {
            print = print + "Yay!";
            } 
            Console.WriteLine(print);
        }
    }
}

However when I uncomment the try...catch lines (for task 2 of 3) suddenly I get an error:

Program.cs(19,31): error CS0103: The name `numInput' does not exist in the current context
Compilation failed: 1 error(s), 0 warnings

..where line 19 is the line that starts with 'for'

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");
            try
            {
            var numInput = int.Parse(Console.ReadLine());
            }
            catch(FormatException error)
            {
            //Console.Write(“You must enter a whole number.”);
            }
            string print = "";
            for(int i = 0; i< numInput ; i++)
            {
            print = print + "Yay!";
            } 
            Console.WriteLine(print);
        }
    }
}

So should that 'for' line be nested inside a finally somehow (maybe):

https://msdn.microsoft.com/library/k4hea629%28v=vs.100%29.aspx


The other confusing thing (and maybe I'm just over-thinking things...) is the Documentation video seemed to go out of it's way to show the use of TryParse when exploring that stackoverflow thread around time index 05:08.

.

Wouldn't using TryParse (instead of int.Parse) obviate the need to catch the FormatExecption?

https://teamtreehouse.com/library/c-basics/perfect/documentation

3 Answers

When you declaring "numInput" string variable in try block, it only exists in that block only. It does not mean anything outside of it. And that's why you got that scoping error. Just put your rest of the code inside your try block and everything will be fine. Just like that.........

Console.Write("Enter the number of times to print \"Yay!\": ");
        try
        {
            var numInput = int.Parse(Console.ReadLine());
            string print = "";
            for (int i = 0; i < numInput; i++)
            {
                print = print + "Yay!";
            }
            Console.WriteLine(print);
        }
        catch (FormatException error)
        {
            Console.Write("You must enter a whole number.");
        }

Thanks. That gets me past task 2 of 3, but then task 3 of 3 says:

Challenge Task 3 of 3

Add more input validation to your program by printing “You must enter a positive number.” if the user enters a negative number.

So I'm still wondering why TryParse (which seems to be faster and catches the exception) isn't the right way to go.

I found this article that says it has definite performance advantages:

http://blog.codinghorror.com/tryparse-and-the-exception-tax/

Did a little more research in trying to do adjust catch for negative numbers (task 3 of 3)

and found this exception handling post which seems to recommend two catch statements.

http://www.dreamincode.net/forums/topic/200234-exception-handling/

So I'm wondering if this is the way to go....will have to play around with it when I have more time..

For those who are looking for help with part 3 of 3 for the final course challenge this forum thread might be helpful:

https://teamtreehouse.com/community/cant-pass-this-task