Visual Studio has a powerful and convenient code input design, and the template is very commendable. You should have used the following statement modules:
if (bool) { }
while (true) { } switch (enum) { } try { } catch (Exception) { throw; }
How to implement quick input of these statements? In the if mode, enter if and press the tab key to generate
if (bool) { }
Here, we will achieve this effect, because in actual development projects, we often encounter a large number of repetitive input, but some words are different, the overall structure is the same. For example:
If (sqldatareader1 ["columnname"] = dbnull. value) {txt_columnname.text = ""; cbo_columnname.selectedindex = 0; lbl_info.text = "no data";} else {txt_columnname.text = sqldatareader1 ["columnname"]. tostring (); cbo_columnname.value = sqldatareader1 ["columnindex"]. tostring (); lbl_info.text = "data available ";}
In the preceding example, you only need to change "columnname", "columnindex", and lbl_info each time. However, it is boring to write dozens of fields like this.
Now we can use resharper to generate a template: (of course, not only resharper can be created, but also vs can be used, but there are some differences in some processes)
1. Select resharper --- Live Templates
2. Right-click user templates and choose new
3. Write the following code in the displayed editing window.
If (sqldatareader1 ["$ columnname $"] = dbnull. value) {TXT _ $ columnname $. TEXT = ""; CBO _ $ columnname $. selectedindex = 0; $ lbl_info $. TEXT = "no data";} else {TXT _ $ columnname $. TEXT = sqldatareader1 ["$ columnname $"]. tostring (); CBO _ $ columnname $. value = sqldatareader1 ["$ columnindex $"]. tostring (); $ lbl_info $. TEXT = "data available ";}
The effect is as follows:
The parameter list on the right shows three variable parameters: columnname, lbl_info, and columnindex. Then, assign a shortcut key to the template defined in the top cut, just like the "if" in the IF mode, we can assign the number 1.
Description: Enter the description of the template. When the "everwhere" superchain is opened, the scope of the template can be defined as follows:
It is easy to understand the options such as language, version, and syntax context compatibility.
4. Finally, save the template file we defined. In the CS file of any project, press 1 and click the tab key. Then, you will find the structure we defined, variable parameters are switched by using the tab. After a parameter is assigned a value, all the required parameters have changed. How convenient is it:
So far, we can create our own template file. In the future, there will be a lot of repetitive work in the project, and there will be no headache.
Good luck!