how to pass array between 2 forms

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Araatn
    New Member
    • Nov 2009
    • 2

    #1

    how to pass array between 2 forms

    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
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    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

    Comment

    • Araatn
      New Member
      • Nov 2009
      • 2

      #3
      how do u mean ?? bc its my second year of vb.net and multiple forms is all new to me

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        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.

        Code:
        using System;
        using System.Collections.Generic;
        using System.Windows.Forms;
        using System.Drawing;
        using System.Threading;
        using System.Data;
        using System.ComponentModel;
        using System.Reflection;
        using System.Security.Permissions;
        using Microsoft.Win32;
        
        namespace MyAmazingProgram
        {
            static class Program
            {
                public static string[] MyStringArray;// Visible to all forms
        
                /// <summary>
                /// The main entry point for the application.
                /// </summary>
                [STAThreadAttribute]
                static void Main(string[] commandLine)
                {
                    Control.CheckForIllegalCrossThreadCalls = false;
                    Application.EnableVisualStyles();
                    //Application.SetCompatibleTextRenderingDefault(true);
                    App myApp = new App();
                    myApp.Run(commandLine);
                }
            }
        In this example "MyStringAr ray" 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:

        Code:
        string ThirdItem = Program.MyStringArray[2];

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          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 "Programmin g 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.

          Comment

          Working...