Reference a CLASS
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Reference a CLASS

  1. #1
    Join Date
    Apr 2014
    Posts
    3

    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

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,091

    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

  3. #3
    Join Date
    Apr 2014
    Posts
    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!

  4. #4
    Join Date
    Apr 2014
    Posts
    3

    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

This is a CodeGuru survey question.


Featured


HTML5 Development Center