开发者

C# and Variable Scope in Winforms

Within a Winform app, I would like data in an instantiated class to be accessible by multiple form controls.

For example, if I create Class Foo, which has a string property of name, I'd like to instantiate Foo a = new a() by clicking Button1, and when I click Button2, I'd like to be able to MessageBox.Show(a.name). There may be multiple instances of Foo, if that开发者_运维百科 matters at all.

What is my best option for being able to use class instances in such a way?


A private field or property of a class satisfies the requirement - such field can be accessed by all methods of the class.

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

namespace Test
{
    public partial class Form1 : Form
    {
        foo a;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            a = new foo();
            a.name = "bar";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (a != null && a.name != null)
                MessageBox.Show(a.name);
            else 
                MessageBox.Show("");
        }
    }

    public class foo
    {
        public string name { get; set; }

        public foo() { }
    }
}

If you want this variable to be accessible to other forms you'd need to make it public (preferably as property) - C# winform: Accessing public properties from other forms & difference between static and public properties


maybe you just want a static class


Winforms are nothing but some graphical elements backed by code. The code can own/create objects just like regular 'non-winform' code. The same scoping rules apply.

My guess if yours is more a problem of 'how does my form access shared state defined outside of it'? Create a static class, or have a setter on the class of your form that other code can use to set this shared state.


A form you create is just another class, derived from Form. A class exists in a given namespace, so you just need to create your Foo class in a namespace shared by your application's forms.

If a class is shared by multiple forms, then usually you'd separate out that class into a separate file.


Jon Skeet [C# MVP] Guest Posts: n/a

2: May 15 '07

re: Application variables in csharp.


On May 15, 12:04 pm, Control Freq wrote: Quote: Still new to csharp. I am coming from a C++ background. > In C++ I would create a few top level variables in the application class. These are effectively global variables which can be accessed throughout the application because the application object is known. > What is the csharp equivalent of this practice? I can't seem to add variables to the "public class Program" class and get access to them from other files. > I am probably missing something obvious. Basically you want public static fields (or preferably properties).

An alternative to this is a singleton: http://pobox.com/~skeet/csharp/singleton.html

Jon

When in doubt, find something signed by John Skeet. Found on: http://bytes.com/topic/c-sharp/answers/646865-application-variables-csharp

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜