Solved

Referencing a function in another class using vs 2003

Posted on 2006-06-14
1
190 Views
Last Modified: 2010-05-18
I am currently converting to visual studio 2003 from vs 2005, however I have encountered a problem with the Shared functions.

In 2005 I can reference a Friend sub from another form, In 2003 I get an error saying 'Reference to a non-shared member requires an object reference.
 
If I create an instance of the form in the second form the error message goes away but this creates a new reference of the form and does not display the correct info. How can I reference a function in another class using vs 2003?
Any ideas? thank you.
0
Comment
Question by:Kmeli
1 Comment
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
Comment Utility
In VS2005, the concept of a "default instance" was brought back into the mix.  This works exactly the same way as it did in VB6...you can use the Forms name to reference the default instance and it can be accessed from anywhere.

VS2002/VS2003 had no such feature.

In those versions you must have a reference to the actual instance of the form you want to deal with.

If you just created the form then you already have an instance.  As in...

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim f1 As New Form1
        f1.Show()
        f1.TextBox1.Text = "something"
    End Sub

But if you are attempting to go the other way, as in updating the form that created "f1" in the previous example, from "f1" itself then you need to pass in a reference to that form.  One way to do this is to use the Owner() property:

    Public Class Form1
        Inherits System.Windows.Forms.Form

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim f2 As New Form2
            f2.Owner = Me
            f2.Show()
        End Sub

    End Class

    Public Class Form2
        Inherits System.Windows.Forms.Form

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim f1 As Form1 = CType(Me.Owner, Form1)
            f1.TextBox1.Text = "Form2 calling..."
        End Sub

    End Class

If you don't want to use Owner() then you can pass in a Reference like this:

    Public Class Form1
        Inherits System.Windows.Forms.Form

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim f2 As New Form2
            f2.f1 = Me
            f2.Show()
        End Sub

    End Class

    Public Class Form2
        Inherits System.Windows.Forms.Form

        Public f1 As Form1

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If Not (f1 Is Nothing) Then
                f1.TextBox1.Text = "Form2 calling..."
            End If
        End Sub

    End Class

Another approach is to use the Singleton pattern.  This is useful if you will only have ONE instance of your form open at any time.  See my discussion and example here:
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21845440.html




0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now