views:

339

answers:

5

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: 

maybe you just want a static class

Brad
Would a static class be suitable in cases where I need multiple instances of the class? I will be opening up several files, and parsing out the relevant information into various properties of the class. I figured a static class would only be suitable if I was not working with several instances.
Brandon
A: 

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.

psychotik
A: 

There's a number of options available to you but here's probably the simplest built in VS 2005

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
    {

        string _name;
        public string name
        {
            get {
                return _name;
            }
            set {
                _name = value;
            }
        }

        public foo()
        { 

        }
    }
}
Conrad Frix
His second requirement is that `a` be available from different forms. EDIT: Or Not?
ChaosPandion
This was exactly what I was looking for. I was able to create a class, and instantiate a List<myClass> within the form scope, and it is accessible by all form objects. Beautiful, thank you!
Brandon
Chaos, while I WOULD like it to be, it is not a necessity. Any further information that would make it accessible by multiple forms would be great.
Brandon
A: 

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 Seigel
A: 

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

Joel Etherton