我在 VB.NET 中有這段代碼:
Dim Hello As String = "123"
temp = Fix_this(hello)
Function Fix_this(The_content As String)
The_content = "456" : Return "test"
End Function
我希望函式Fix_This更改字串變數“Hello”,甚至不知道該變數的名稱。我知道我可以回傳一個新的 Hello 字串,但我想回傳其他內容。
功能上有沒有辦法Fix_this,它可以找出“The_content”的來源?
所以我的目標是fix_this找出 The_content 的來源是“Hello”,然后能夠動態更改函式中的 Hello 值。不知道我是否有意義?
所以假設我成功了,我得到了字串值“Hello”。然后我該如何更改 hello 值?例如:
Source_of_The_content = "Hello"
如果我這樣做:
Source_of_The_content = "New"
它顯然不會將“Hello”字串值更改為 New。
uj5u.com熱心網友回復:
如果您希望 Function 或 Sub 修改其接收的引數,請將引數宣告為ByRef. 使用您的Fix_this功能:
Function Fix_this(ByRef The_content As String) As String
The_content = "456"
Return "test"
End Function
如果運行以下代碼,The_content將是“456”:
Dim Hello As String = "123"
MsgBox("Hello before Fix_this: " & Hello)
Dim temp As String = Fix_this(Hello)
MsgBox("Hello after Fix_this: " & Hello)
MsgBox("temp: " & temp)
如果您只是對修改感興趣,也可以使用 Sub 來執行此操作The_content:
Sub Fix_this(ByRef The_content As String)
The_content = "456"
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/466999.html
標籤:VB.net
