Do we convert values just to be more efficient?

VitzzViperzz

Well-known member
Joined
Jan 16, 2017
Messages
75
Location
United Kingdom
Programming Experience
1-3
Hey,

I was just following a book (Worx C# for beginners) and it's been very fun so far. However, it tells us to convert the value of the user's input! After looking at MSDN, it seems it's just better for "efficiency", but is that it?

I would think that there is more to it than that because it seems pointless when writing smaller programs.

Here is the code:

So we declare FirstNumber and Second number as a double.

C#:
 double FirstNumber, SecondNumber;

Then we are converting that to a double again. It seems a little odd, don't you think?

C#:
FirstNumber = Convert.ToDouble(Console.ReadLine());
SecondNumber = Convert.ToDouble(Console.ReadLine());

It would be great if someone explained this.

Thanks!
 
No, you aren't converting that to double "again". This line:
C#:
double FirstNumber, SecondNumber;
declares two variables as type 'double'. That is saying that there are two places with those names that can store 'double' values. That means that, if you want to store data in those two places, that data must be 'double' values.

These lines:
C#:
FirstNumber = Convert.ToDouble(Console.ReadLine());
SecondNumber = Convert.ToDouble(Console.ReadLine());
call the ReadLine method of the Console class to get a line of data entered by the user. What is the return type of that method? It's 'string'. The reason that it's 'string' is that the user can enter any characters they want. If your variables are type 'double' then can they store 'string' values? No, they can't. The purpose of the conversion is to get 'double' values from those 'string' values so that you have something of the correct type to store in the variables. Without that conversion, the compiler will tell you that you can't assign a 'string' to a variable of type 'double'.

The main reason for that is to stop you trying to use data in a manner that is not appropriate for its type, e.g. trying to perform mathematical operations on data that does not represent numbers. It can only do so much though. The code you have is calling Convert.ToDouble and that will satisfy the compiler that you have considered your data types but, if the user enters text that cannot be converted to a 'double' at run time then the code will still fail. For that reason, something like Convert.ToDouble should only be used when you know for sure that the data can be converted that way. For something like user input, you should always validate to make sure. In this case, you can use Double.TryParse to validate and convert in one action, e.g.
C#:
if (double.TryParse(Console.ReadLine(), out FirstNumber)
{
    // The data was valid and the result is contained in FirstNumber.
}
else
{
    // The data was invalid.
}
By the way, it is convention to begin local variables with a lower-case letter, so this:
C#:
double firstNumber, secondNumber;
would be more appropriate.
 
Hey, thanks for the detailed reply!

I now understand why we use the Convert function. I will also start by using lowercase letters for local variables.

Once again, many thanks.
 
Back
Top Bottom