close
動態參數陣列
當不能正確的得知要傳遞的陣列元素多寡時。
您可能希望能夠在執行階段具有改變陣列大小的能力。
ParamArray (Visual Basic)
ParamArray 可讓您將任意數目的引數傳遞至程序。
當按下按鈕一時取出最大值100
當按下按鈕二時取出最大值200
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim retval_A As Integer = Getmax(3, 5, 100, 80) 'Getmax把四個陣列元素丟給x()
Label1.Text = Val(retval_A)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim retval_B = Getmax(7, 9, 200, 82, 99) 'Getmax把五個陣列元素丟給x()
Label1.Text = Val(retval_B)
End Sub
Function Getmax(ByVal ParamArray x() As Integer) As Integer
Dim max As Integer
max = x(0) '先假設x(0)為最大值
For i = 0 To x.Length - 1 'i從0到4-1
If x(i) > max Then
max = x(i)
End If
Next
Return max
End Function
End Class
全站熱搜
留言列表