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

ali raafat
ali raafat
444 Points

can any one explain it to me again i cant get it all.

anyone can explain it to me plz

CodeChallenge.cs
int.Parse height = int.height ;
Mark Miller
Mark Miller
45,831 Points

The left side of your equation should only be a declaration of existence, an empty new variable to contain something of your choice. Then use assignment, the equals sign, to say what its value should be. Parsing input will return a value on the right side of the equals, and as long as both sides are of the same type of data, the assignment will be accepted.

Mark Miller
Mark Miller
45,831 Points

You are given a string of heightInput with a value that has been put in by someone, but it's a string. To convert this string into an integer data type, you use the Parse() method, which is a static method of the int class. You can pass the string into the parenthesis and the return value will be an integer, which can be assigned to the newly declared integer named height, all in one line. A string can't take math operations, that's why the conversion is needed.

int height = int.Parse(heightInput);

Mark Miller
Mark Miller
45,831 Points

At the start of the challenge, heightInput is of type string, it's a string. The goal is to convert it to an integer type of variable. So, declare a new variable of type int, and name it height. Then, assign the value of this new variable, of type int, to be the value returned from using the Parse() method of the int wrapper class. You can call this method by the dot operator on the int class. No object is needed, it's a static method of the wrapper class named int. You should pass in the string identified as heightInput into the parenthesis of the Parse() method of the int wrapper class. Static methods do not require creation of an object, the class name can invoke the method. In this case the class name is int. The returned value is assigned to be the value of the new variable you declared, and good news, the two types match!

1 Answer

andren
andren
28,558 Points

If you want a detailed explanation I would recommend re-watching Converting Strings to Integers, which is the video that preceded this challenge. They show how to use the int.Parse method in that video, and all of the code needed to solve this challenge is featured.

Anyway to summarize the video, int.Parse is a function that will return an integer version of a string passed in to it. To solve this task you have to create an int variable called height in the same way you normally would. And then set it equal to the int.Parse method with the heightInput variable as the argument. Like this:

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