动态变量变化 VB.NET

Dynamic variable change VB.NET

我在 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”,甚至不知道该变量的名称。我知道我可以 return 一个新的 Hello 字符串,但我想 return 其他东西。

有没有办法在Fix_this函数中找出“The_content”的来源?

所以我的目标是 fix_this 函数找出 The_content 的来源是“Hello”,然后能够动态更改函数中的 Hello 值。不知道我说的有没有道理?

假设我成功了,我得到了字符串值“Hello”。我该如何更改 hello 值?例如:

Source_of_The_content =   "Hello"

如果我这样做:

Source_of_The_content  = "New"

显然不会将“Hello”字符串值更改为 New。

如果你想让一个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)

如果您只是想修改 The_content:

,也可以使用 Sub 来完成此操作
Sub Fix_this(ByRef The_content As String)
    The_content = "456"
End Sub