-
April 6th, 2014, 04:18 AM
#1
Reference a CLASS
I have a problem switching back and forth between 2 classes.
If I use the following code in Class1, I have no problem activating Class2.
(Dim myClass2 As New Class2)
However, if I put a similar line in Class2,
(Dim myClass1 As New Class1)
it causes a looping condition that crashes with a stack fault.
I think what I need to do is not declare a new class1 inside class2, and instead pass in a reference to my existing class1. The problem is I don't know how to code that.
If anyone can offer any example of how that's done I sure would appreciate it.
Thanx!
Visual Basic Express Edition 2010
-
April 8th, 2014, 10:04 AM
#2
Re: Reference a CLASS
You're almost there
OK, do you have a constructor for your Class2?
If you have, you have to supply an argument to it, then create an object of the same type and set this newly created object = to the argument.
Here is an example ( using FORMS )
Make a new Windows Forms project.
Add a Second form by clicking Project, Add Form
Add one button on each form
In the Solution Explorer, make sure that you click on "show all files"
You'll see Form2's Designer becomes visible, and open its code window
In the Code Window, click on New in the Method drop down list ( the one on the right )
This will give you the NEW method for Form 2, then edit it to look like this :
Code:
Public Sub New(parent As Form1)
' This call is required by the designer.
InitializeComponent()
MyParent = parent
' Add any initialization after the InitializeComponent() call.
End Sub
Open the code Window for Form 2, and add the following :
Code:
Private MyParent As Form1
Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
MyParent.Show()
End Sub
Inside Form 1, call your second form as such :
Code:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim F2 As New Form2(Me)
F2.Show()
Me.Hide()
End Sub
I hope it helps
-
April 9th, 2014, 08:15 AM
#3
Re: Reference a CLASS
Thanx for you response.
I should have mentioned in my original post that the project I am working on is a "class library" and not a "windows forms application".
I tried your code, creating a forms app. and it worked fine.
However, trying to use your code in my class library didn't go so well.
I'm getting "Show" and "Hide" and "InitializeComponent()" errors.
If you can help with that I'd appreciate it.
Thanx!
-
April 9th, 2014, 08:31 AM
#4
Re: Reference a CLASS
Hold the phone!
I think I figured out the changes I had to make and it looks like it's working.
Thanx for all 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
|