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.
- 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 "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:
-
string ThirdItem = Program.MyStringArray[2];
-