1

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.

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

4 Comments

this is awesome and perfect. Just what I needed. One question though. How can I do this without using function? How can I change a variable without changing it directly by just knowing the name of the variable in vb.net? Thank you !!!!!
Take a look at CallByName. You can create a property for your variable like Public Property hello As String = "123" and then update it like this: CallByName(Me, "hello", CallType.Let, "456")
@Maria, why do you think that you need modify a variable based on name stored as a string? It's very rare that you should need to do that so, if you think you do, it's probably the result of bad design.
@Maria If you want to be able to access values by name, you should set up the data structure for managing it yourself. For what you are discussing, I would recommend looking at a Dictionary(Of String, String).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.