Connecting Tech Pros Worldwide Forums | Help | Site Map

how to pass array between 2 forms

Newbie
 
Join Date: Nov 2009
Posts: 2
#1: 3 Weeks Ago
hey everyone

i want to know how u can pass an array between multiple forms in vb.net

like example for code

form2
public test(2) as string

test(0) = "test1"
test(1) = "test2"
test(2) = "test3"

form1
how can i get the value of my array test(1) of form2 ???
txtTest.text = ................................

tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,772
#2: 3 Weeks Ago

re: how to pass array between 2 forms


It might just be cleaner to put it in your Program.cs as public then any form can access it along these lines.

Program.MyArray[3] for example
Newbie
 
Join Date: Nov 2009
Posts: 2
#3: 3 Weeks Ago

re: how to pass array between 2 forms


how do u mean ?? bc its my second year of vb.net and multiple forms is all new to me
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,772
#4: 3 Weeks Ago

re: how to pass array between 2 forms


An object created with a method has a scope of life only as large as the method.
If you create an object outside of a method then it has a scope of life as large as the class that it is in.
So if you create an object inside the Program class then it exists as long as the class Program exists.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows.Forms;
  4. using System.Drawing;
  5. using System.Threading;
  6. using System.Data;
  7. using System.ComponentModel;
  8. using System.Reflection;
  9. using System.Security.Permissions;
  10. using Microsoft.Win32;
  11.  
  12. namespace MyAmazingProgram
  13. {
  14.     static class Program
  15.     {
  16.         public static string[] MyStringArray;// Visible to all forms
  17.  
  18.         /// <summary>
  19.         /// The main entry point for the application.
  20.         /// </summary>
  21.         [STAThreadAttribute]
  22.         static void Main(string[] commandLine)
  23.         {
  24.             Control.CheckForIllegalCrossThreadCalls = false;
  25.             Application.EnableVisualStyles();
  26.             //Application.SetCompatibleTextRenderingDefault(true);
  27.             App myApp = new App();
  28.             myApp.Run(commandLine);
  29.         }
  30.     }
  31.  
In this example "MyStringArray" is accessable to any form in your project by referencing it through the class it is a part of: "Program"

So inside Form 1 you could:

Expand|Select|Wrap|Line Numbers
  1. string ThirdItem = Program.MyStringArray[2];
  2.  
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,772
#5: 3 Weeks Ago

re: how to pass array between 2 forms


Quote:
bc its my second year of vb.net and multiple forms is all new to me
So in more than a year of class plus whatever time you spend on your own playing and experimenting with this you've never created an application with more than one form? <stunned>

Get your money back on the tuition and go to the local book store. Buy a couple books of "Programming VB in 21 days", "Visual Basic in 15 minutes a day" and so on. You'll teach yourself more out one of those books in a week, than you learned in class in the first year.
Reply