If I want to transfer var1 in form1 to form2 to be used in an alogrithm how do I do this?

I have tried importing the form and then using the variable - no success.
I have tried something to this effect form1.var1 - Get the error that var1 is not accessible due to it being private, yet var1 is declared and given a value in a public function.

Is there a general setting that I need to set to make variables global?

Any guidance would be of great help.

Many Thanks.

Recommended Answers

All 4 Replies

If you want to set an instance variable an any Object (including a Form), the variable itself must be public. The access level of the function it is instantiated in doesn't matter.

If it is declared in a function, then it is a local variable to that function, and inaccessible to every other function (with the exception of pass-by-reference of course).

Sorry I meant it is declared in the Public Class <name> section of the code

Public Class Form2
    Dim var1 As Integer

    Public Sub size_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    End Sub

    Public Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        var1 = 1
        Me.Hide()
        Form1.BringToFront()
    End Sub

    Public Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        var1 = 2
        Me.Hide()
        Form1.BringToFront()
    End Sub

    Public Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        var1 = 3
        Me.Hide()
        Form1.BringToFront()

    End Sub
End Class

This is the section from Form1 where it is required.

 Public Function fSize()
        psize.Visible = True
        psize.BringToFront()
        Dim p1 As Integer = NanzzaEPOS.form2.var1


        Return True
    End Function

So I cannot see how it is not publicly declared. I have read some people have said that you need to set mdiContainer to true on the main form, I have not tried this yet, is that a likely solution?

The class is public, but each instance variable can have an access modifier as well; see Dim Statement and Access Levels in Visual Basic for more info. When you don't explicitly give it an access modifier, it takes on a default access level (Private in this case); see Declaration and Contexts and Default Access Levels for more info on that. In short, you have to put Public in front of it, like so:

Public Class Form2
    Public Dim var1 As Integer
    ...
End Class

As a side note, Dim becomes optional when you use most keywords in a variable declaration, like this:

Public Class Form2
    Public var1 As Integer
    ...
End Class
Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim frm As New Form2
        frm.Psize = 100
    End Sub

End Class

Public Class Form2
    Public Property PSize As Integer

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        MsgBox.Show(PSize.ToString())
    End Sub

End Class

Hope that helps!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.