Drag four Label controls from the toolbox to the interface, and modify the Text attribute to "Name:, age:, result:, and message ", note the last Label (that is, the NAME attribute of the Label control whose Text attribute is "message );
Xiaotian: when there are so many data types, they must be used together. For example, we ask the user to enter the user name and age and then display the result.
Lao Tian: You 'd better use WinForm to do this. Because this is only the second operation, let's talk about the steps below:
1. Create a Windows desktop application project named "type conversion and unpacking;
2. drag four Label controls from the toolbox to the interface, and modify the Text attribute to "Name:, age:, result:, and message ", note the last Label (that is, the NAME attribute of the Label control whose Text attribute is "message );
3. Drag two TextBox controls to the interface and modify the NAME attribute of the control to tb_name and tb_age;
4. Drag a Button control and modify the Text attribute to submit;
5. Final effect 2-17
Figure 2-17
6. After completing the preceding steps, double-Click the submit button to generate a Click event for this button;
7. The Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Linq;
Using System. Text;
Using System. Windows. Forms;
Namespace type conversion and unpacking
{
PublicpartialclassForm1: Form
{
Public Form1 ()
{
InitializeComponent ();
}
/// <Summary>
/// Double-click the submit button to generate an event
/// </Summary>
/// <Param name = "sender"> the object that triggers the event. Of course, this type of event is the current object. </param>
/// <Param name = "e"> message attached to the event </param>
Privatevoid button#click (object sender, EventArgs e)
{
// The following lines of code are written by us. Other types are automatically generated except for comments. Note the following two conversions.
// Because the Text attribute of tb_name is also of the string type, values can be directly assigned without type conversion.
String name = tb_name.Text;
// Use the ToInt32 method of the Convert class to Convert the string type value received in tb_age to the int type.
Int age = Convert. ToInt32 (tb_age.Text );
// Msg is equal to name and int type age. Because of the different types, age is converted to string type.
String msg = name + age. ToString ();
// Modify the Text attribute of Label4 used to display messages
Label4.Text = msg;
}
}
}
8. Run the program (go back to chapter 1 if you forget it );
9. Final running result 2-18
Figure 2-18
Xiao Tian: No wonder. I have demonstrated exactly the same thing as you. Why am I wrong? You see 1-19
Figure 2-19
Laotian: is it good? The 30 you entered can be converted to an integer, right? But you can say "30 years old", how can you switch this C? The conversion fails. This is common in data type conversion.
Xiaotian: How do I know what can be switched, and what can't be switched?
Laotian: Of course, the conversion of data types is regular. From the perspective of conversion form, data types can be divided into two types: implicit conversion without syntax statements and explicit display conversion with other classes or methods.
From the perspective of the conversion type, it can be divided into extended conversion and narrowing conversion. The so-called extended conversion refers to the conversion of a type with a small range and digits to a large one. For example, the byte type is converted to the int type. Type downgrading.
This article is a new one. For more information, see the source and author!