code driving me crazy, any assistance?

Discussion in 'ASP .Net' started by andyjgw@gmail.com, Jun 29, 2005.

  1. Guest

    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
    , Jun 29, 2005
    #1
    1. Advertising

  2. 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.


    <> wrote in message
    news:...
    > 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
    >
    Kevin Spencer, Jun 29, 2005
    #2
    1. Advertising

  3. AndrewW Guest

    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... :(
    AndrewW, Jul 1, 2005
    #3
  4. 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.

    "AndrewW" <> wrote in message
    news:...
    > 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... :(
    >
    Kevin Spencer, Jul 1, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. William Gower

    ItemTemplate error driving me crazy

    William Gower, Apr 23, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    320
    William Gower
    Apr 23, 2004
  2. =?Utf-8?B?VGltOjouLg==?=

    Insert and Variables driving me crazy! Newbie...

    =?Utf-8?B?VGltOjouLg==?=, Dec 20, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    337
    =?Utf-8?B?VGltOjouLg==?=
    Dec 21, 2004
  3. Shapper
    Replies:
    0
    Views:
    332
    Shapper
    Apr 29, 2005
  4. Joe Blanchard via .NET 247
    Replies:
    1
    Views:
    762
    Eliyahu Goldin
    May 15, 2005
  5. Replies:
    1
    Views:
    490
    Cowboy \(Gregory A. Beamer\)
    Dec 5, 2005
Loading...

Share This Page