Beginners programming friends often like to collect some very "wonderful" programming skills, however, the accumulation of skills often did not improve the quality of the program, but to guide some programmers blindly pursuit of strange and new, forget the basic programming habits of training, not conducive to teamwork, perhaps, this is China is not lack of intelligent programmers, But one reason for the lack of a smart development team. In the development of ASP.net, we can learn a lot of skills, but some basic programming habits we must develop, so that not only can fundamentally improve the quality of the program and development efficiency, but also conducive to the reading of the program and team development. If you write the program only you can read or only a few people can understand, even if the program skills gadget its technology, for the program upgrades and maintenance are fatal problems.
I. Processing of errors (other than)
The most basic requirement of program robustness is the process and capture of program error, in asp.net, the error processing has the same mechanism as other programming languages, you can use try ... Catch... Finally and so on, this is compared with the ASP has a greater progress. Moreover, using these error-handling methods can greatly improve the readability of the program and the speed of the program debugging, in the combination of these advantages, we should pay more attention to this point.
For error handling, we can refer to this article (in English):
http://www.123aspx.com/redir.aspx?res=28336
Second, the processing of strings
In web design, the processing of strings is almost the most common. With ASP.net, strings are processed faster than ASPs, and in ASP.net, a string-handling class Stringbulider is used to perform some common string operations, and the most important, using StringBuilder can greatly increase string processing speed.
In ASP.net, the most common use of "&" is to connect two strings:
Dim myOutputString As String = "My name is"
Dim myInputString As String = " Alex"
myOutputString = myOutputString & myInputString
Response.Write(myoutputString)
Now, let's look at the use of StringBuilder, when using StringBuilder, we can do some basic operations on strings, such as Append, Replace, Insert, remove, and so on, now we look at specific examples.
(1) The use of append in StringBuilder
Append, like the Append in other languages, is the addition of other characters at the end of the string.
Dim sb as StringBuilder = New StringBuilder()
sb.append( "<table border='1' width='80%'>" )
For i = 0 To RowCount - 1
sb.Append("<tr>")
For k = 0 To ColCount - 1
sb.Append("<td>")
sb.Append( dt.Rows(i).Item(k, DataRowVersion.Current).toString())
sb.Append( "</td>" )
Next
sb.Append("<tr>")
Next
sb.Append( "</table>")
Dim strOutput as String = sb.ToString()
lblCompany.Text = strOutput
In the above program, using the Append method to achieve a table output, it should be noted that Stringbulider must first use the ToString () method to convert it to a string type can be directly output. In the above example, all we see is append a direct string, in fact, this method has a very convenient function, that is, you can directly append other types of variables, such as directly appemd an integer type of value, of course, Our output is automatically converted to a string later:
Sub Page_Load(Source As Object, E As EventArgs)
Dim sb As System.Text.StringBuilder
Dim varother As Integer
varother=9999
sb =new System.Text.StringBuilder()
sb.append("<font color='blue'>可以Append其他类型:</font>")
sb.append(varother)
Response.write(sb.toString())
End Sub
(2) Use of other methods in the string
We can also use other methods, we take a look at the common:
Insert method, you can insert other characters at the specified location, using the method: Insert (insert position, insert character);
Remove method, you can delete the specified number of characters at the specified location, using the method: remove (In fact, the number of characters);
Replace method, which replaces the specified character, using the method: replace (substituted string, replacement string)
You can refer to the following articles for specific descriptions and usage of strings:
Http://aspfree.com/aspnet/stringbuilder.aspx
Http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemTextStringBuilderClassTopic.asp