Ivan Kvolik
Ivan Kvolik

Reputation: 431

Visual basic sets the property which was given as an argument

I have a very puzzling situation that i have discovered and i hope someone with deeper knowledge of VB.NET can help me. I have a method which looks like this

Public Function DoSomeWork(ByRef argument) As Boolean
    ...
    Return true
End Function

I call this method the following way

DoSomeWork(SomeObject.IntegerProperty)

My question is why does Visual Basic try to assign value to the SomeObject.IntegerProperty when "Return True" is called. Property is assiged with the same value it had earlier. Just to kae it clear i am not changing argument in any way i just take its value and assign it to some other property of totally unrelated object.

Upvotes: 0

Views: 46

Answers (1)

JMichael
JMichael

Reputation: 564

This may be me misunderstanding the question, but it sounds like you're saying that your application sets the value of SomeObject.IntegerProperty. I'm not sure how you know this, but given that you're saying that DoSomeWork shouldn't alter SomeObject.IntegerProperty, the simple answer is to replace ByRef with ByVal. This will force the function to treat IntegerProperty as Read-only.

Upvotes: 1

Related Questions