FAQ: Data Types and Variables - Converting Data Types

We are now more than one year passed the original notification about how confusing this lesson is. Yet this error is still not repaired. I just tried it, and I got the same message as the other users.
Are we not paying enough each month to at least have the decency to fix this?

At the end after running “int faveNumber = Convert.ToInt32(Console.ReadLine());” on the last excercise I get a green checkmark. My question is after running it again am I suppose to get an error? Or is that part of the excercise?

Are you getting the error at the end of the excercise as well?

Ok. This exercise should really be broken into three exercises and explained a little more since it is so early in the course.

First, I was confused because the exercise states that we are converting a string into an integer. Well, that doesn’t mean you type your answer to the question “What is your favorite number” as a string, for example “Nine”.

We are really learning (and it would help to explain it better) that when a user enters anything into your prompt it is treated as a string. So typing “9” as the answer turns “9” into a string.

Our actual challenge in this exercise is to convert “9” the string into 9 the integer. Maybe that was clear to everyone, but I come from different coding and this took a minute to figure out.

Now the exercise gets complicated, if you follow the instructions it will fail about twice. You are learning what doesn’t work, that is the purpose of the exercise up to this point. It is supposed to fail. So you learn what doesn’t work.

Then you are given guidance to use a Convert.ToX() method. In this case Convert.ToInt32().

So, for the grand finale, you are supposed to use:

int faveNumber = Convert.ToInt32(Console.ReadLine());

Then once you run… ta da … nothing happens.

That is the outcome you want. No errors. Congratulations.

Another person posted a better finish to this by adding:

Console.Write(“Is “);
Console.Write(” your favorite number?”);

This gives you something to see after the conversion is successful.

It’s not a bad exercise, it’s just abstract in how it is presented. It really helped me understand what they mean by an explicit conversion, so it accomplished that task.

Hey There, I’m confused with what the initial data type for faveNumber is supposed to be. Is it a string or an int? because either way the input from the user will be a string anyway, which then gets converted to an int.

What does this 32 mean and is it different in different apps if it is how do we know what is the number there.


And the next question:
I am trying to make a program that asks you what is your favorite number and you can answer with either int or double but shows an error. How to fix it?

What are they trying to teach us in this lesson, not sure what the Convert.ToInt32… is supposed to accomplish since they don’t explain what that code means.

Can someone clarify? Thanks

The 32 in Int32 is the number of bits (the number is stored in binary, so there are 32 spaces for 1 or 0 to make up the number). This gives a value range of -2147483648 to 2147483647. One if the bits is for denoting if the number is positive for negative. You can also have 64 (long), 16(short) and 8(sbyte), along side the unsigned versions (can only be positive numbers) which would be ToUInt. Also might be worth noting that Byte and sbyte are ToByte/SByte rather than int8/uint8.

You’re error is you have done Convert.ToDouble20, which doesn’t exist. It shouldn’t have the 20 on the end, it is just ToDouble.

Convert.ToInt32 will try to change(convert) the given value into a 32 bit signed integer. In this case it will try to change a string into an integer. As has been mentioned above it can only convert a sign (+ or - which is optional) followed by digits, it cannot convert Nine into 9, or if the value is null it will return 0. If the string cannot be converted it will throw a FormatException.

Why would you want to convert the string into the int? You can’t do maths with the string but you can with the int.

Thank you I appreciate the response…

Hey Brian, can you explain to me why I can’t use (int)Console.ReadLine(); to convert like the course guide but when I tried Convert.ToInt32(Console.ReadLine());, it worked? I really appreciate your help!

I also struggled with this tutorial, because I thought the purpose was to change a decimal value to a integrer… But it seems the exercise was to convert the input into a string value?

This is my interpretation…

"If you tried dotnet run again, you’ll see that (int) didn’t work either. That’s because it is not possible to implicitly convert a string into an int (or vice versa) in C#."

Is this sentence a typo? In the instructions, we are told to use (int) to cast a string to int. Based on previous information in the tutorial, that is an example of explicit conversion. So the sentence should read:

"If you tried dotnet run again, you’ll see that (int) didn’t work either. That’s because it is not possible to explicitly convert a string into an int (or vice versa) in C#."


I need help… I did “int faveNumber = Convert.ToInt32(Console.ReadLine());” and it still gives me the error saying it can’t convert the string into an integer.

Wow! tysm! Your solution is great!