variable declaration in form1 accessible in form2...

G

Guest

Hello everybody,

I want to declare an array in form1, load it in form1 and then access that
array in form2. New in VB, It took me a while to understand that declaring
variables right under class form1 make those variables accessibles everywhere
in that form1:

Public Class Form1
Inherits System.Windows.Forms.Form
Dim TextLine(66) As String
...

So my question is: how to declare variables in form1 that will be accessible
in form2, form3 & etc. ?

Thank you
 
C

CT

Declare the variable as Public, instead of Dim. Then reference using
form1.TextLine. Anotehr option is to have it as a global variable in a
Module.
 
C

Chris

Marcel said:
Hello everybody,

I want to declare an array in form1, load it in form1 and then access that
array in form2. New in VB, It took me a while to understand that declaring
variables right under class form1 make those variables accessibles everywhere
in that form1:

Public Class Form1
Inherits System.Windows.Forms.Form
Dim TextLine(66) As String
...

So my question is: how to declare variables in form1 that will be accessible
in form2, form3 & etc. ?

Thank you

It depends how your forms are set up. Is Form2/3 created in Form1? If
so there is two easy ways to go about it. Either pass in a reference of
Form1 and create a property in form1 to access the array... Or create a
property in Form2 and set it from form1.

this example forces the array to be passed into form2 when form2 is
created.
public class form2
Dim TextLine() As String

sub new (Arr as Array)
TextLine = Arr
end sub
end class


Chris
 
G

Guest

Hi Carsten,

Public Class Form1
Inherits System.Windows.Forms.Form
Public TextLine(66) As String

Public Class Form2
Inherits System.Windows.Forms.Form

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Button1.Text = Form1.TextLine(1)
End Sub

At form1.textline(1) I have the syntax error message: reference to a
non-shared member requires an object reference.
 
C

Chris

Marcel said:
Hi Carsten,

Public Class Form1
Inherits System.Windows.Forms.Form
Public TextLine(66) As String

Public Class Form2
Inherits System.Windows.Forms.Form

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Button1.Text = Form1.TextLine(1)
End Sub

At form1.textline(1) I have the syntax error message: reference to a
non-shared member requires an object reference.

This is because you don't have a reference to the object created as
form1. look at my post below.
 
G

Guest

Sorry Chris, I really dont understand your solution. I am probably missing
very basic concepts.
 
C

Chris

Marcel said:
Sorry Chris, I really dont understand your solution. I am probably missing
very basic concepts.
Any my first question and I'll be able to help you more.

What is the relationship between form1 and form2. Is form1 creating
form2 and doing a form2.showdialog or form2.show?

Chris
 
G

Guest

Hi Chris,

Finally, in form1, it has to be defined Public SHARED...

Thank you very much for all of your help.
 
G

Guest

Public Class Form1
Inherits System.Windows.Forms.Form
Public Shared TextLine(66) As String

It is working when it is define public SHARED...

Thank you
 
C

Chris

Marcel said:
Public Class Form1
Inherits System.Windows.Forms.Form
Public Shared TextLine(66) As String

It is working when it is define public SHARED...

Thank you

Make sure you read up on what the "Shared" keyword does for variables in
a form.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top