|
-
May 31st, 2004, 08:54 PM
#1
Thread Starter
Lively Member
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?
-
May 31st, 2004, 10:05 PM
#2
Sleep mode
I don't think VB.NET work this way !
-
May 31st, 2004, 10:27 PM
#3
Thread Starter
Lively Member
???
Can you make some suggestions then? How do I execute a function of a specific instance of a class from another class?
-
Jun 1st, 2004, 09:37 AM
#4
Sleep mode
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 .
-
Jun 1st, 2004, 01:35 PM
#5
Thread Starter
Lively Member
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.
-
Jun 1st, 2004, 09:56 PM
#6
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:
'all this code is in Form6
Private _mo As MyObject
Public Sub New(ByVal mo As MyObject)
Me.new()
_mo = mo
End Sub
Private Sub Form6_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not _mo Is Nothing Then
'this shows the private data from the same instance that was passed in to the constructor
MsgBox(_mo.GetInternal)
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'this is from another form but it can be from anywhere
'also i am using the constructor to pass the instance but it can be however you want
Dim sameInst As New MyObject("This is going to be a private variable!")
Dim f As New Form6(sameInst)
f.Show()
End Sub
'here is the class which is not in form6
Public Class MyObject
Private _InternalVariable As String = String.Empty
Public Function GetInternal() As String
Return _InternalVariable
End Function
Public Sub New(ByVal internal As String)
_InternalVariable = internal
End Sub
End Class
-
Jun 2nd, 2004, 12:12 AM
#7
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|