Data Types, Casting, and Parsing
We learned in Chapter 6 that we can declare variables with a data type and then assign values to them in our code. We learned that we can accept user input and assign the input value to a variable, and we saw that there are times when we will need to convert a variable from one data type to another. In this context we learned about narrowing and widening conversions, and we used a conversion from an int to a byte, which involved casting, for example, (byte).
In C#, casting is a method used to convert one data type to another. Casting is used as an explicit conversion and tells the compiler what to do, but we need to be aware that there may be a loss of data. So we use casting to achieve a numeric conversion where the destination data type we are assigning the value to is of a lesser precision. Casting is a conversion from one numeric data type to another numeric data type as discussed earlier with narrowing and widening.
Integral types
Integral types | |||
---|---|---|---|
byte | Byte | 1 | May contain integers from 0 to 255 |
sbyte | SByte | 1 | Signed byte from –128 to 127 |
short | Int16 | 2 | Ranges from –32,768 to 32,767 |
ushort | UInt16 | 2 | Unsigned, ranges from 0 to 65,535 |
int | Int32 | 4 | Ranges from –2,147,483,648 to 2,147,483,647 |
uint | UInt32 | 4 | Unsigned, ranges from 0 to 4,294,967,295 |
long | Int64 | 8 | Ranges from –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
ulong | UInt64 | 8 | Unsigned, ranges from 0 to 18,446,744,073,709,551,615 |
So taking a data type in Table 7-1 and trying to assign it to a variable with a data type above it in the table means we are moving to a less precise format and therefore we must use a cast, casting. Now we will code an example of int to short.
- 1.
Right-click the solution CoreCSharp .
- 2.
Choose Add.
- 3.
Choose New Project, as shown in Figure 7-1.
- 4.
Choose Console App from the listed templates as shown in Figure 7-2.
- 5.
Click the Next button.
- 6.
Name the project Chapter7, leaving it in the same location, as shown in Figure 7-3.
- 7.
Click the Next button.
- 8.
Choose the framework to be used, which in our projects will be .NET 6.0 or higher, as shown in Figure 7-4.
- 9.
Click the Create button.
- 10.
Right-click the Chapter7 project in the Solution Explorer panel.
- 11.
Click the Set as Startup Project option.
- 1.
Double-click the Program.cs file in the Chapter6 project.
- 2.
Highlight ALL the code within the program, yes, including the namespace and Main method.
- 3.
Choose Copy.
- 4.
Double-click the Program.cs file in the Chapter7 folder.
- 5.
Highlight the existing code and delete it.
- 6.
Right-click inside the blank editor window.
- 7.
Choose Paste.
- 3.
Click the Change namespace to Chapter7 option, as shown in Figure 7-8.
- 4.
Press the Enter key.
The namespace will now have been renamed to Chapter7.
- 1.
Amend the code to add the two variables, as in Listing 7-1.
Two variables of types int and short with initial values
- 2.
Click the File menu.
- 3.
Choose Save All.
We will now assign the value of the maximumAmountForRepairCosts variable to the variable called maximumAmountForCarHire.
- 4.
Amend the code, as in Listing 7-2.
Assign one variable value to another variable
- 5.
Amend the code, as in Listing 7-3, to display the values of maximumAmountForRepairCosts and maximumAmountForCarHire.
Display messages including the variables
As we see in Figure 7-9, the variable called maximumAmountForRepairCosts within the assignment statement is underlined with red. So we need to fix this issue, once we understand what is causing the error.
- 6.
Hover over the red underline and read the pop-up message, as in Figure 7-9.
- 7.
Amend the code, as in Listing 7-4, to perform the casting.
Cast int type to short type
Now we should see that the red underline error has disappeared. We have fixed the conversion issue by using a cast to make the value of the variable of data type int into a short value.
- 8.
Click the File menu.
- 9.
Choose Save All.
- 10.
Click the Debug menu.
- 11.
Choose Start Without Debugging.
- 12.
Type Ford as the manufacturer name.
- 13.
Press the Enter key on the keyboard.
- 14.
Type Fiesta as the model name.
- 15.
Press the Enter key on the keyboard.
- 16.
Type Blue as the vehicle color.
- 17.
Press the Enter key on the keyboard.
- 18.
Type 5 as the vehicle age.
- 19.
Press the Enter key on the keyboard.
- 20.
Type 6999.99 as the estimated vehicle value.
- 21.
Press the Enter key on the keyboard.
- 22.
Type 50000 as the number of kilometers on the odometer of the vehicle.
- 23.
Press the Enter key on the keyboard.
- 24.
Type 1998-01-01 as the date of birth for the main driver of the vehicle.
- 25.
Press the Enter key on the keyboard.
- 26.
Press the Enter key to close the console window.
If we amend the code so that the int variable is set to have a value that is one more than the maximum value of the short data type, 32768, there will be an issue. This means the casting will take place with loss of accuracy.
- 27.
Amend the code as in Listing 7-5.
Assign a value of 32768 to a short – this is outside the type range
- 28.
Click the File menu.
- 29.
Choose Save All.
- 30.
Click the Debug menu.
- 31.
Choose Start Without Debugging.
- 32.
Click in the console window.
- 33.
Type Ford as the manufacturer name.
- 34.
Press the Enter key on the keyboard.
- 35.
Type Fiesta as the model name.
- 36.
Press the Enter key on the keyboard.
- 37.
Type Blue as the vehicle color.
- 38.
Press the Enter key on the keyboard.
- 39.
Type 5 as the vehicle age.
- 40.
Press the Enter key on the keyboard.
- 41.
Type 6999.99 as the estimated vehicle value.
- 42.
Press the Enter key on the keyboard.
- 43.
Type 50000 as the number of kilometers on the odometer of the vehicle.
- 44.
Press the Enter key on the keyboard.
- 45.
Type 1998-01-01 as the date of birth for the main driver of the vehicle.
- 46.
Press the Enter key on the keyboard.
- 47.
Change the value back to 32767 from 32768.
Amend the code to use a Boolean data type and use parsing for the conversion.
- 1.
Amend the code, as in Listing 7-6, to add two new variables, one of data type string and the other of data type bool (Boolean).
Add variables of types string and bool
- 2.
Amend the code, as in Listing 7-7, to ask the user for input.
Ask for user input
- 3.
Amend the code, as in Listing 7-8, to assign the value entered at the console to the variable fullyComprehensiveRequirement.
Read the string input and assign it to a variable
- 4.
Amend the code, as in Listing 7-9, to display the value of the variable fullyComprehensiveRequirement, which is of data type bool.
Display the bool value
Hovering over the word Console, we will be presented with an error message. We could also hover over the ReadLine() part of the statement and we will get a similar message. We are being told that fullyComprehensiveRequirement cannot be implicitly converted from a string data type to a bool data type. We need to fix the issue by performing a conversion as the message says. There is no talking about a cast as we saw in the previous example because this is not a conversion from one numeric data type to another numeric data type.
- 5.
Amend the code, as in Listing 7-10, to perform the parsing.
Parse the string input to a bool
Now we should see that the red underline error has disappeared as we fixed the conversion issue .
- 6.
Click the File menu.
- 7.
Choose Save All.
- 8.
Click the Debug menu.
- 9.
Choose Start Without Debugging.
- 10.
Click in the console window.
- 11.
Type Ford as the manufacturer name.
- 12.
Press the Enter key on the keyboard.
- 13.
Type Fiesta as the model name.
- 14.
Press the Enter key on the keyboard.
- 15.
Type Blue as the vehicle color.
- 16.
Press the Enter key on the keyboard.
- 17.
Type 5 as the vehicle age.
- 18.
Press the Enter key on the keyboard.
- 19.
Type 6999.99 as the estimated vehicle value.
- 20.
Press the Enter key on the keyboard.
- 21.
Type 50000 as the number of kilometers on the odometer of the vehicle.
- 22.
Press the Enter key on the keyboard.
- 23.
Type 1998-01-01 as the date of birth for the main driver of the vehicle.
- 24.
Press the Enter key on the keyboard.
- 25.
Type True for the answer to the question. It is true that we require fully comprehensive insurance.
Using the Convert class ToBoolean() method to parse
Chapter Summary
In this chapter we have learned about the particularly important programming concepts of parsing and casting and have seen the “subtle” difference between them. We have seen that in parsing we use the wrapper class that represents the data type, for example, Boolean, and we noted that this class, like all classes, starts with a capital letter. Each wrapper class has methods that will perform the parsing, for example, Parse(), and the method will be passed the name of the variable to be parsed, converted.
We are making really great progress, and we should keep it foremost in our programming thoughts that data types, casting, parsing, and conversions are a core programming concept and will be used throughout the chapters in this book, but more importantly, they are widely used in commercial applications.
We are making great progress in our programming of C# applications, and in finishing this chapter and increasing our knowledge, we are advancing to our target.