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..

CodeChallenge.cs
string heightInput = "168";
string height = heightInput;
int.Parse(heightInput);

1 Answer

Jennifer Nordell
STAFF
Jennifer Nordell
Treehouse Staff

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 Staff

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