要返回多個(gè)值,有幾種辦法,最直接的就是用傳址參數(shù)
成都創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供松北網(wǎng)站建設(shè)、松北做網(wǎng)站、松北網(wǎng)站設(shè)計(jì)、松北網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、松北企業(yè)網(wǎng)站模板建站服務(wù),十多年松北做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
Function f1(byref retVal1,byref retVal2)
或者用全局變量(比如模塊級變量)
或者返回包裝過的對象
或者返回多個(gè)返回值糅合的字符串
Public?Class?Form1
Public?Class?SquareClass?'把多線程調(diào)用的函數(shù)封裝到類中,通過類事件返回
Public?Value?As?Double
Public?Square?As?Double
Public?Event?ThreadComplete(ByVal?Square?As?Double)
Public?Sub?CalcSquare()
Square?=?Value?*?Value
RaiseEvent?ThreadComplete(Square)
End?Sub
End?Class
Dim?WithEvents?oSquare?As?SquareClass
Private?Sub?Button1_Click(sender?As?Object,?e?As?EventArgs)?Handles?Button1.Click?'多線程返回值測試,當(dāng)線程運(yùn)行完成激發(fā)事件
oSquare?=?New?SquareClass()
Dim?t?As?New?Threading.Thread(AddressOf?oSquare.CalcSquare)
oSquare.Value?=?30
t.Start()
End?Sub
Sub?SquareEventHandler(ByVal?Square?As?Double)?Handles?oSquare.ThreadComplete?'響應(yīng)事件函數(shù)
MsgBox("The?square?is?"??Square)
End?Sub
End?Class
不但可以返回兩個(gè)值,還可以更多。例如 4 個(gè)。
Private Sub Command1_Click()
Dim My1 As Boolean, a1 As Long, a2 As String, a3 As Long
My1 = MyFun(1,a1, a2, a3)
'顯示返回的 4 個(gè)變量的值
Me.Cls
Me.Print My1
Me.Print a1
Me.Print a2
Me.Print a3
End Sub
Private Function MyFun(S as long ,a1 As Long, a2 As String, a3 As Long) As Boolean
MyFun = True
a1 = 100
a2 = "MyStr"
a3 = "200"
End Function