473,406 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

variable declaration in form1 accessible in form2...

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

Nov 21 '05 #1
9 5236
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.

--
Carsten Thomsen
Communities - http://community.integratedsolutions.dk

"Marcel Saucier" <Ma***********@discussions.microsoft.com> wrote in message
news:11**********************************@microsof t.com...
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

Nov 21 '05 #2
Marcel Saucier wrote:
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
Nov 21 '05 #3
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.

--
Super Basic programmer under DOS since 1983. Absolutely dummy (& new) VB.NET
programmer under Windows since september 2005 ! Most of you will agree to say
that it was about time to... upgrade !
"CT" wrote:
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.
"Marcel Saucier" <Ma***********@discussions.microsoft.com> wrote in message

news:11**********************************@microsof t.com...
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


Nov 21 '05 #4
Marcel Saucier wrote:
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.
Nov 21 '05 #5
Sorry Chris, I really dont understand your solution. I am probably missing
very basic concepts.
--
Super Basic programmer under DOS since 1983. Absolutely dummy (& new) VB.NET
programmer under Windows since september 2005 ! Most of you will agree to say
that it was about time to... upgrade !
Nov 21 '05 #6
Marcel Saucier wrote:
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
Nov 21 '05 #7
Hi Chris,

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

Thank you very much for all of your help.

--
Super Basic programmer under DOS since 1983. Absolutely dummy (& new) VB.NET
programmer under Windows since september 2005 ! Most of you will agree to say
that it was about time to... upgrade !
"Chris" wrote:
Marcel Saucier wrote:
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

Nov 21 '05 #8
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


--
Super Basic programmer under DOS since 1983. Absolutely dummy (& new) VB.NET
programmer under Windows since september 2005 ! Most of you will agree to say
that it was about time to... upgrade !
"Marcel Saucier" wrote:
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.

--
Super Basic programmer under DOS since 1983. Absolutely dummy (& new) VB.NET
programmer under Windows since september 2005 ! Most of you will agree to say
that it was about time to... upgrade !
"CT" wrote:
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.
"Marcel Saucier" <Ma***********@discussions.microsoft.com> wrote in message

news:11**********************************@microsof t.com...
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


Nov 21 '05 #9
Marcel Saucier wrote:
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.
Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: YGeek | last post by:
Is there any difference between declaring a variable at the top of a method versus in the code of the method? Is there a performance impact for either choice? What about if the method will return...
5
by: MMSJED | last post by:
I am beginner in using C#, actually I am trying to move from VB6 to C# I need very small help in programming problem my be you will laugh when you get it That simply I have to form let’s say...
6
by: Sergey Muschin | last post by:
Hi there, how to handle a forward declaration in managed VC ? for example, let say i'm working with to form classes form1 and form2. Now, how can i call a public method of form2 from form1? I...
13
by: MJ | last post by:
as topic, if i wan to create an array where the content of the array can be edited by form1 and form2, how i going to do it? for example the content of array is {1,2,3} form2 change the content...
5
by: John | last post by:
Hi, I can't find a simple example for a simple(?) problem. I am working on an application with a variable in form1, that variable is needed in form2 for a calculation but i can't get that...
3
by: Vijay | last post by:
I have 2 forms. in form1 i declared a public variable in which a value is stored. then i goto form2 . (for this is create an object of form2 in form1.) when i go to form2 there is call form1. when...
11
by: Miro | last post by:
I am banging my head around something that I think I just dont understand what I am reading in the helps. Its gotta be so simple but I just cant figure out what im reading. ( vb 2003 ) Lets...
3
by: Joseph | last post by:
I would like to know how to pass a variable from one Windows Form1 to Form2? Coming from VB background, it was a simple matter of creating a module and declaring the variable this made the...
7
by: Boki | last post by:
Hi All, I want to change WindowState of form1 from form2. I tried these two methods, but no luck on both. (1) Declare a public method: /* function of form1 */ public void...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.