Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
See more:
How do you access form variable from a module in vb.net?
Posted

Declare the form variable as public.
 
Share this answer
 
First, the variable should be declared public (to access from the same of different assembly) or internal (to access from the same assembly, so it should be used more often). This "variable" is actually should be a member, either a field or a property. Usually, proper encapsulation requires it to be a property, not a field. Having any fields accessible from outside is considered a bad coding style.

If the member is static, you access it from outside as MyModule.MyMember. If this is an instance member (non-static), you need to have an instance of the module (via a constructor you call somewhere which returns you the reference to the instance), and access it as MyInstance.MyMember.

—SA
 
Share this answer
 
Comments
Marc A. Brown 16-Mar-12 14:26pm    
Nice explanation.
Sergey Alexandrovich Kryukov 16-Mar-12 14:36pm    
Thank you, Marc.
--SA
You cannot directly access a class without a reference to an instance of that class. Your best bet might be something like this:

VB
Module Module1
    Private m_MyForm As Form1
    Public ReadOnly Property MyForm() As Form1
        Get
            If IsNothing(m_MyForm) Then m_MyForm = New Form1
            Return m_MyForm
        End Get
    End Property
    Public Sub WriteLog(ByVal txt As String)
        MyForm.TextBox3.Text += txt + vbNewLine
    End Sub
End Module


Now anywhere in your application you can access Form1 using Module1.MyForm.
 
Share this answer
 
Comments
Tony Radu 16-Mar-12 11:26am    
Thanks man!!
You read a book or watch a training video to learn about the tools before using them.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900