code driving me crazy, any assistance?

A

andyjgw

Hi

I posted this in the caching section, before noticing that it appears
to be a practically dead NG, so please excuse the x-post...

Can anyone advise on this please? Something is happening here and it's
driving me mad trying to work out what. For simplicity I've reduced all
code to a simple section running in my applications' Page_Load method:

(in declarations:
Private svcinfo As New serviceInfo
)

Private Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Cache.Item("ServiceInfo") Is Nothing Then
svcinfo = svcIMS.getServiceInfo
Cache.Insert("ServiceInfo", svcinfo)
Debug.WriteLine("Envelope in the cache is " &
Cache("ServiceInfo").env.ToString)
Else
' got one already thanks, so reload that.
svcinfo = Cache("ServiceInfo")
End If

cE = svcinfo.env
cE.minx = 10
cE.maxx = 20
cE.miny = 100
cE.maxy = 200

Debug.WriteLine("Changed local cE to be " & cE.ToString)
Debug.WriteLine("Envelope now in the cache is " &
Cache("ServiceInfo").env.ToString)
End Sub

And here's the output I get. The initial load in the cache is the value
I want to preserve, it should never be changed.

Envelope in the cache is SW: 166000,828000 : NE 232000,921000
Changed local cE to be SW: 10,100 : NE 20,200
Envelope now in the cache is SW: 10,100 : NE 20,200

The toString function for an Envelope is:
Overrides Function toString() As String
Return String.Format("SW: {0},{1} : NE {2},{3}", _minx, _miny,
_maxx, _maxy)
End Function

How come I have a local copy of what is in the cache (in cE) yet when I
change cE it affects what's in the cache? I don't get it... :(

Someone put me out of my misery please :)

Cheers
AW
 
K

Kevin Spencer

Hi AW,

svcinfo = Cache("ServiceInfo")

AFAIK, the "=" operator is an assignment operator, NOT a "copy" operator.
;-)

Here's the long explanation: An object is a reference type. What this means
is that an object is basically a pointer to a data structure. A variable is
a container for data. It is not the data itself, although it might look like
one due to programming syntax. So, when you assign the reference to an
object to a variable, you are saying that the variable now references that
object. A pointer is an address in memory. It "points to" the beginning of a
data structure. So, when you assign svcinfo to Cache("ServiceInfo") you are
assigning the same memory address to your variable; IOW, it now references
the same object.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 
A

AndrewW

Sorry, meant to reply to this yesterday.

That put me on the right track - my problem was indeed as you said. I
fixed it by extending my class to implement ICloneable so now I'm
cloning the object before assignment.

Thanks very much for your help Kevin, much appreciated. Taught me an
important concept that had given me the slip.

Ironic however in that in another part of the project, I had been
cloning another object so as to deal with this sort of problem. Having
done that, it would appear I slipped the blinkers straight back on
again... :(
 
K

Kevin Spencer

Don't be too hard on yourself, Andrew. We all slip the blinkers on now and
again!

You're only in real trouble if Uncle Ernie drops his chopsticks!

--
;-),

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top