tailieunhanh - Beginning Microsoft Visual C# 2008 PHẦN 2

Các loại chuyển đổi rõ ràng bạn đã được sử dụng trong rất nhiều các thử ví dụ trong cuốn sách này là một chút khác nhau từ những người bạn đã nhìn thấy cho đến nay trong chương này. Bạn đã được chuyển đổi các giá trị chuỗi thành số bằng cách sử dụng các lệnh như (), | Chapter 5 More About Variables Figure 5-2 The property you want to change is one of the Advanced settings so you click the Advanced button. In the dialog that appears enable the Check for arithmetic overflow underflow option as shown in Figure 5-3. By default this setting is disabled but enabling it provides the checked behavior detailed previously. Figure 5-3 99 Part I The C Language Explicit Conversions Using the Convert Commands The type of explicit conversion you have been using in many of the Try It Out examples in this book is a bit different from those you have seen so far in this chapter. You have been converting string values into numbers using commands such as which is obviously something that won t work for every possible string. If for example you try to convert a string like Number into a double value using then you will see the dialog shown in Figure 5-4 when you execute the code. Figure 5-4 As you can see the operation fails. For this type of conversion to work the string supplied must be a valid representation of a number and that number must be one that won t cause an overflow. A valid representation of a number is one that contains an optional sign that is plus or minus zero or more digits an optional period followed by one or more digits and an optional e or E followed by an optional sign and one or more digits and nothing else except spaces before or after this sequence . Using all of these optional extras you can recognize strings as complex as as being a number. There are many such explicit conversions that you can specify in this way as the following table shows Command Result val val converted to bool val val converted to byte val val converted to char val val converted to decimal val val converted to double val val converted to short val val converted to int val val