Results 1 to 7 of 7

Thread: Passing address of an instance of an object

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2001
    Posts
    80

    Passing address of an instance of an object

    Man... where's a pointer when you need one

    Here's my situation, I have an instance of some Class A and that class creates an instance of a Form. After the control is passed to the Form object it has to contact the parent Class A and run a function or two. So my question is, how do I pass the address of the object to the form so that when the form executes a function from that object it's from the same instance and not a new one? I've tried passing the class ByRef to a function in the form and then that function had its own variable of the same type defined, but without an instance. Then I'd set that variable to the one that was passed ByRef and try to execute it, but then I got a message saying that the object doesn’t have an instance. Tried it with a delegate as well but that is for functions only and I need something for a class.

    Any ideas?

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I don't think VB.NET work this way !

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2001
    Posts
    80
    ???

    Can you make some suggestions then? How do I execute a function of a specific instance of a class from another class?

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by MXK
    ???

    Can you make some suggestions then? How do I execute a function of a specific instance of a class from another class?
    You would call it like any other function in a class . I'm sure you know how to do this . I didn't understand what you're trying to do but constructors can help maybe. they fire upon each instantiation of the class .

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2001
    Posts
    80
    I don't want another instance of the class. The initial class has specific private variables in it and functions called from that class need those variables to work properly. That’s why I have to call a function from a specific instance which has those variables set.

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You don't even need ByRef you are automatically passing a reference to the object. I'm not sure what you are doing and if you need more help you should post your code but here is a brief example:
    VB Code:
    1. 'all this code is in Form6  
    2.  
    3.     Private _mo As MyObject
    4.  
    5.     Public Sub New(ByVal mo As MyObject)
    6.         Me.new()
    7.         _mo = mo
    8.     End Sub
    9.  
    10.     Private Sub Form6_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    11.  
    12.         If Not _mo Is Nothing Then
    13.             'this shows the private data from the same instance that was passed in to the constructor
    14.             MsgBox(_mo.GetInternal)
    15.         End If
    16.  
    17.     End Sub
    18.  
    19.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    20.  
    21.         'this is from another form but it can be from anywhere
    22.         'also i am using the constructor to pass the instance but it can be however you want
    23.         Dim sameInst As New MyObject("This is going to be a private variable!")
    24.         Dim f As New Form6(sameInst)
    25.         f.Show()
    26.  
    27.  
    28.     End Sub
    29.  
    30.  
    31. 'here is the class which is not in form6
    32.  
    33. Public Class MyObject
    34.  
    35.     Private _InternalVariable As String = String.Empty
    36.  
    37.     Public Function GetInternal() As String
    38.         Return _InternalVariable
    39.     End Function
    40.  
    41.     Public Sub New(ByVal internal As String)
    42.         _InternalVariable = internal
    43.     End Sub
    44.  
    45. End Class

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Oct 2001
    Posts
    80
    Yea I actually figured it out now, I should've just used ByVal when passing the class. Thanks for your help.

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