Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pass an array to a class method and make sure contents don't change

Status
Not open for further replies.

T0AD

Programmer
Jun 4, 2003
73
GB
I've created a class, and I'm passing an array to one of it's methods. The problem is, the original array's contents change even though the argument is declared ByVal. The code in the method only refers to the array that's passed as an argument, not the original... does anyone know why the original one is changing?


There's a thin line between genius, and insanity!
 
Using ByVal to pass an array only means that the array itself is protected from change. The contents of the array can be changed by the code in the function.

If you want to make a copy of the array, look at calling the .Clone method before passing it to your function. Note the warning in the MSDN documentation that the copy is only a shallow copy, not a deep copy.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Okay I'll check it out. Thanks for your help.

There's a thin line between genius, and insanity!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top