Solved

Referencing a function in another class using vs 2003

Posted on 2006-06-14
1
192 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
ID: 16906127
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

770 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