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) Perform Converting Strings to Integers

Needed help with this question. (CodeChallange)

Someone, please help me with this! the question says: Convert the string in heightInput to an integer using int.Parse. Store the result in a variable named height. I tried everything and I could not get it to work..

string heightInput = "168";
string height = heightInput;

1 Answer

Jennifer Nordell
Jennifer Nordell
Treehouse Teacher

Hi there, Ibrahim! There are a couple of things going on here. First, we're trying to turn that string into an integer and then store the integer version of it in the variable height, but you've declared height to be of type string. Secondly, you do use the int.Parse method, but the result of that is neither displayed nor saved anywhere. It's just sort of floating out there in limbo.

Steps to take:

  • Declare height to be type int
  • Set height to be equal to the result of running int.Parse on heightInput

I think you can get it with these hints, but let me know if you're still stuck! :sparkles:

I'm sorry but I still don't understand..

Jennifer Nordell
Jennifer Nordell
Treehouse Teacher

Ok, for Step 1 you only need one additional line of code:

int height = int.Parse(heightInput);

This line declares a variable named height. We then use the equals sign to indicate an assignment. On the right side of the equals is what is assigned to the variable. In this case, we're taking the string that was stored in heightInput, converting it to an integer, and assigning the result to height. At the end, the value in height will be 168 as opposed to "168". This means that later we can perform mathematical operations on that value.

Hope this clears things up! :sparkles:

Oh! i figured it out, i had to think better... :smile: thanks for the help! appreciate it