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!

Local variable assignment won't stay local!

Status
Not open for further replies.

alanf

Programmer
Feb 7, 2001
39
US
Hi.
I have an application with a global array of objects (Routes). In a function, I declare a local route:
Dim RouteLocal as Route
Set RouteLocal = New Route

I then assign a value (a route) from my global array to the local route:
Set RouteLocal = gRtPd(ndx)

When I manipulate a value in RouteLocal, it alters the value in the public array gRtPd(). Why? The whole point of the local route was to manipulate the values without affecting those in the global array. How can I change this behavior?
 
I am going to throw out a guess here. I am not sure what the inner workings of VB actually do with an array but I am assumming it is similar to C in that it is actually a pointer. (Again this is speculation, if someone knows for sure please post it as I would like to know for sure). I am guessing that by your statement:

Set RouteLocal = gRtPd(ndx)

You are actually creating a reference to gRtPd(ndx) which in turn makes them a reference to the same address, therefore a change to one would also change the other. Does any one know this for sure? Anything is possible, the problem is I only have one lifetime.[thumbsup2]
 
You can do both pass it by Reference or Value....VB
defaults....by Reference...
in your argument...you need to say something like ByVal

like :
Public Function SquareFn(ByVal pintArg As Integer) As Long
SquareFn = pintArg * pintArg
End Function

does this help?
 
Thanx ggreg,

I knew the ByVal and ByRef when you pass a variable to a procedure, but do you know if that is how VB handles a Set keyword? Anything is possible, the problem is I only have one lifetime.[thumbsup2]
 
ggreg,
Thanks for your input. The problem is, I'm not passing the array to the function. It's a global array.
 
alanf,

I found this:

Generally, when you use Set to assign an object reference to a variable, no copy of the object is created for that variable. Instead, a reference to the object is created. More than oneobject variable can refer to the same object. Because such variables are references to the object rather than copies of the object, any change in the object is reflected in all variables that refer to it. However, when you use the New keyword in the Set statement, you are actually creating an instance of the object.

Sound like if you do this:

Set RouteLocal = New gRtPd(ndx) You should be able to edit RouteLocal without changing gRtPd(ndx). I did not try it with the array of objects, but if you would let me know if it works.

Tom
Anything is possible, the problem is I only have one lifetime.[thumbsup2]
 
Thanks, Tom. Actually, that syntax doesn't work - it returns an error message. With the New keyword, VB wants a class name, and gRtPd(ndx) is not a class name but a reference to a specific object.

I also tried, in the calling function, to create a new local Route object, and then pass it ByVal to this function, and even that didn't work. The Global array still got changed!

I'm beginning to think I will need to get rid of the global array, declare it locally in my main function, and pass it from function to function, ByVal where necessary. Seems like as a global array, I just can't get a local reference to an object in it that's ByVal.

Alan
 
The business about set creating a pointer to an object and not a copy is spot on.
Why not create your local object and then pass over the properties from the global object.
 
I was afraid VB was going to do that. What happens if you use this:

RouteLocal = gRtPd(ndx)

since you Declare RouteLocal as New

Maybe worth a shot?!? Anything is possible, the problem is I only have one lifetime.[thumbsup2]
 
ByRef or ByVal makes no difference. You are still passing a object reference. At the "C" level, the only difference is whether you pass a pointer (ByRef) to the object reference or put the object reference on the stack (ByVal). An object reference still refers to the original object. Whatever you do using that object reference affects the object to which it refers.

What happens:
Dim RouteLocal as Route
' Create New object
Set RouteLocal = New Route
' Destroy created object and set to existing
Set RouteLocal = gRtPd(ndx)

If you want to "play" with a copy then you will need to build in "Clone" function of your own to make a copy into a whole new object.
Public Function CLone() As myobject
Dim objW As myobject
Set objW = New myobject
with objW
.prop1 = mobjprop1
.prop2 = mobjprop2
End With
Set Clone = objW
End Function


Generate Forms/Controls Resizing/Tabbing Class
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Hi folks.

foada: that generates an error: object does not support this property or method. You just can't assign an object without the set keyword.

Beard: I could do that. The oject is a linked list, so if there were a lot of items in the list it could be time-consuming. The whole objective is to make this as fast as possible. And this is the main calculation routine in the program that will get repeated many times. So I'm really trying to avoid iterating through the lists an extra time.

Alan
 
JohnYinling,
Thanks for your input. Which leads to doing what Beard had suggested, so I guess that's what I need to do. Thanks everyone.

Alan
 
Alan,

Just a question for you. What are you doing that made you stay away from building a collection of Routes and went with the Array instead? Just curious. Anything is possible, the problem is I only have one lifetime.[thumbsup2]
 
Tom,
I have been focused on maximizing the speed of the application everywhere possible, as there are many calculations to be performed. My primary use of the array is to access an object by index, and in my research, I determined that this is much faster with an array than with a collection.
 
That is what I thought. Thanks for the reply. Anything is possible, the problem is I only have one lifetime.[thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top