CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2014

    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.

    Visual Basic Express Edition 2010

  2. #2
    Join Date
    Jul 2001
    Sunny South Africa

    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 :

    Public Sub New(parent As Form1)
            ' This call is required by the designer.
            MyParent = parent
            ' Add any initialization after the InitializeComponent() call.
        End Sub
    Open the code Window for Form 2, and add the following :

    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
        End Sub
    Inside Form 1, call your second form as such :

     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim F2 As New Form2(Me)
        End Sub
    I hope it helps

  3. #3
    Join Date
    Apr 2014

    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.

  4. #4
    Join Date
    Apr 2014

    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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.