Home > database >  Dynamic variable change VB.NET
Dynamic variable change VB.NET

Time:04-29

I have this code in 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

I want function Fix_This to change the string variable "Hello" without knowing the name of that variable even. I know I can return a new Hello string but I want to return something else.

Is there a way in Fix_this function, it can figure out the source of the "The_content" ?

So my goal is fix_this function to find out the source of The_content is "Hello" and then being able to dynamically change the Hello value in the function. No idea if I'm making any sense?

So let's say I succeed and I get string value "Hello". How do I then change the hello value? For example:

Source_of_The_content =   "Hello"

If I do this:

Source_of_The_content  = "New"

it obviously is not going to change the "Hello" string value to New.

CodePudding user response:

If you want a Function or Sub to modify the parameter it receives, declare the parameter as ByRef. Using your Fix_thisfunction:

Function Fix_this(ByRef The_content As String) As String
    The_content = "456"
    Return "test"
End Function

If you run the following code, The_content will be "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)

You can also do this by using a Sub if you are just interested in modifying The_content:

Sub Fix_this(ByRef The_content As String)
    The_content = "456"
End Sub
  • Related