简体   繁体   中英

Passing Variables From File-to-File in C#

How would I use a variable declared in Program.cs and access it's value from Form1.cs ?

I know how to do this in C, but I'm completely lost in Microsoft's little twist on C.

Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using LuaInterface;

namespace WindowsFormsApplication1
{
    static class Program
    {
        public static Lua lua = null;
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
            lua = new Lua();
        }
    }
}

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LuaInterface;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            lua.DoString("print('hi')");
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }
    }
}

Using your examples of Program.cs and Form1.cs and assuming these are the default names and that you have a Program class that instantiates a Form1 class and that you want to pass a parameter to the Form1 class, you can do the following:

Define a constructor for Form1 that takes this parameter and chain to the default constructor:

private Lua lua;

public Form1(Lua lua) : this()
{
   this.lua = lua;
}

In your Program class when instantiating Form1 , pass the parameter to it:

lua = new Lua();
Application.Run(new Form1(lua));

Note that I am using OOP terminology - objects and classes (not files).


Update:

Since you have declared your lua variable as a public static member of the Program class, you can access it anywhere in your program (assuming the namespaces have been declared appropriately) as follows:

Program.lua;

Though you would want to instantiate the static field before calling Application.Run .

In any way, this makes the object a public shared resource across all threads - making it virtually untestable and difficult to work with if you go multi-threaded.

You can also access the parameter with

Program.lua;

But Oded's way is cleaner.

And in Main, the object must be instantiated before Application.Run:

    static void Main()
    {
        lua = new Lua();
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

In Program.cs there is most probably a static class defined like this:

static class Program
{
   ...
}

If you declare the variable as public or internal , you will be able to access it from your form.

static class Program
{
    public static int myVariable;
    public static int MyProperty { get; set; }
}

In your form, you access the variable and the property with:

int i = Program.myVariable;
int j = Program.MyProperty;

In object-oriented programming, you would usually keep the variables private and only declare the properties as public.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM