Results 1 to 3 of 3

Thread: Address Question

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2002
    Posts
    382

    Address Question

    I'm wondering if anyone can answer these pretty basic OOP questions dealing with classes and memory addresses..

    Public Class TestClass
    Public s As String
    End Class

    Dim X As New TestClass
    X.s = "Test 1"
    Dim Y As TestClass = X
    Dim Z As TestClass = X

    Am I correct in saying X holds the original copy of TestClass and Y and Z are just Pointers that point to the copy of X?

    So if I...
    Y.s = "Test 2"

    That changes the original "s" in element X right so now (X,Y,Z).s all equal "Test 2"

    Now if I do a

    X = Nothing

    Y.s and Z.s still show the "Test 2" but now where has the Original copy that was in X gone? Are Y and Z still just pointers or does one of them now contain the original ? Im wondering because if X is no longer a object, Y and Z shouldn't be able to access it anymore right??

    I'm confused..

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Try it:
    VB Code:
    1. Dim X As New TextBox
    2.         X.Text = "Test 1"
    3.         Dim Y As TextBox = X
    4.         Dim Z As TextBox = X
    5.         MsgBox(String.Format("Y='{0}' Z='{1}'", Y.Text, Z.Text))
    6.  
    7.         Y.Text = "Test 2"
    8.         MsgBox(String.Format("X='{0}' Z='{1}'", X.Text, Z.Text))
    9.  
    10.         X = Nothing
    11.         MsgBox(String.Format("Y='{0}' Z='{1}' X='{2}'", Y Is Nothing, Z Is Nothing, X Is Nothing))

    I think the answer is that x,y, and z are all just pointers to the address where the class belongs. The 'original copy' is seperate and keeps track of who is referencing it and is not set to nothing, itself, until all references to it are gone or set to nothing.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2002
    Posts
    382
    Ah thanks again Edneeis, that's clears up alot.. So I guess I gotta keep tight tabs on all the pointers

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width