Today in class, to the students to talk about the access2010 process in the call, in the impression of the access2010 materials are mentioned: The process call can be omitted. Can be in the class when the problem arises!!!
First, the situation reappearance
One of the functions of the explanation process: you can reduce the duplication of code to improve sharing efficiency. The following code is used:
Private Sub numSort() ‘产生20个[0,100]之间的整型随机数 Dim n(1 To 20) As Integer Dim i As Integer For i = 1 To 20 Randomize n(i) = Rnd() * 101 Next ‘打印排序前随机数 Dim outString As String outString = "排序前随机数:" For i = 1 To 20 outString = outString & Str(n(i)) & " " Next Debug.Print outString ‘排序 Dim temp As Integer Dim j As Integer For i = 1 To 20 For j = i To 20 If (n(i) > n(j)) Then temp = n(i) n(i) = n(j) n(j) = temp End If Next Next ‘打印排序后随机数 outString = "排序后随机数:" For i = 1 To 20 outString = outString & Str(n(i)) & " " Next Debug.Print outStringEnd Sub
The purpose of the example is to generate 20 random numbers to be stored in an array, to print the array, to order 20 random numbers from small to large, and to print the array again.
In this case, two times the array is printed, the two pieces of code are common, can be introduced into a process to reduce code, improve code reuse rate. So in the classroom began to change the code of the students (originally thought to be simple things, without prior verification, the results of jokes ...) [Email protected]~]. The modified code is as follows:
Private Sub numsort () ' produces an integer random number between 20 [0,100] Dim n (1 to.) As Integer Dim i As Integer For i = 1 to Randomize N (i) = Rnd () * 101 Next ' Print sort before the random number Printarr ("Random number before sorting:", N) ' sort Dim temp as Intege R Dim J as Integer for i = 1 to-j = i to-If (n (i) > N (j)) Then temp = n (i) n (i) = N (j) n (j) = Temp End If Next Next ' print sorted random number End Subprivat E Sub Printarr (s As String, K () As Integer) Dim outstring As String outstring = S Dim i As Integer For i = LBo und (k) to UBound (k) outstring = outstring & Str (k (i)) & "" Next Debug.Print outstringend Sub
In the definition of the Printarr process, to invoke the calls without call, the tragic thing happened, give the following hint:
Obviously Printarr ("random number before sorting:", N) this statement has a problem. But there is no grammatical problem (alas ...). This is the result of the scholarship is not strict, remember, remember, later no longer fan), after the interview the next call Plus, change to:
Call Printarr ("random number before sorting:", N)
Problem Solving!!!
Ii. Summary of issues
I tried it again after a few examples. Found:
If the parameter of the procedure contains an array parameter, call must be added;
Otherwise call can be omitted.
Iii. Lessons Learned
- As a Jiaoshu Jiang, impart to the student's knowledge, oneself must be careful to verify, cautious, cautious, again cautious, verify, verify, re-verify, otherwise easy fraught ...
- Empiricism is not advisable, be sure to remember
Another: For the code more convenient porting, more general, the above code can be optimized (using symbolic constants and using LBound and UBound two system functions)Option Compare DatabasePrivate Const ARR_LOW = 1 ‘数组上标Private Const ARR_UPP = 20 ‘数组下标
Private Sub Numsort ()
Dim N (arr_low to Arr_upp) as Integer
Dim I as Integer
For i = LBound (n) to UBound (n)
Randomize
N (i) = Rnd () * 101
Next
' Print a random number before sorting
Call Printarr ("random number before sorting:", N)
‘排序Dim temp As IntegerDim j As IntegerFor i = LBound(n) To UBound(n) For j = i To UBound(n) If (n(i) > n(j)) Then temp = n(i) n(i) = n(j) n(j) = temp End If NextNext‘打印排序后随机数Call printArr("排序后的随机数:", n)
End Sub
Calls and no call issues in the Access2010 process