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 Numbers Converting Strings to Numbers

Could you parse the return value from Ask instead of making a new variable?

Instead of making a the new variable number to do the math with, couldn't you just parse the value returned by the Ask method?

For example:

  int entry = int.Parse(Ask("How many cans are you ordering?"));
  Console.WriteLine($"For {entry} cans, your total is: ${entry * 2}");

Seems to work fine and, as far as I am aware, should work fine with if/else statements as well. But I'm quite new, so I don't know if this would create issues in different circumstances, or would be considered poor form, etc.

Thank you for your time!

1 Answer

Steven Parker
Steven Parker
231,268 Points

It works fine for this example. But in a more sophisticated program you might want to keep the original reply string to use in possible error messages should the conversion fail.

That definitely makes sense. And I will say that, even by the end of this extremely simple cat food store program, I was already seeing how that might not be the best idea.

Thank you for your response!