CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2003
    Location
    Ft. Worth Texas
    Posts
    31

    [RESOLVED] VS2010/.Net4.0 Global access to a created class

    Hello,

    ref:
    http://www.codeguru.com/forum/showthread.php?t=516547

    Now that I have created a class in Form1 that works, how do I access the stored data in the created instance of the class from another form?

    In other words, The class I created in form1 needs to be globally accessable by all forms. I don't want to create another instance on a different form, it holds no data.

    Can someone point me in the right direction?

    Thanks!

  2. #2
    Join Date
    Feb 2003
    Location
    Ft. Worth Texas
    Posts
    31

    Re: VS2010/.Net4.0 Global access to a created class

    Here's some info:

    This is a couple of forms from my Test2 test project:

    Form1:
    Code:
    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;
    
    namespace Test2
    {
        public partial class Form1 : Form
        {
            public txDataObj TxDataObject = new txDataObj();
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                TxDataObject.NumberOfFields = 5;
            }
    
        }
    
        public class txDataObj
        {
            public string Delimiter;
            public string[] FieldData = new string[128];
            public int NumberOfFields;
            public string TxString;
            public int TransmitInterval;
        }
    }
    Form2:
    Code:
    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;
    
    namespace Test2
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
    
            private void Form2_Load(object sender, EventArgs e)
            {
                textBox1.Text = TxDataObject.NumberOfFields.ToString();
            }
        }
    }
    In Form2, the last line:

    textBox1.Text = TxDataObject.NumberOfFields.ToString();

    The TxDataObject gives this error:
    The name 'TxDataObject' does not exist in the current context.

    The value I set in form1 needs to appear in the textbox on form2.

  3. #3
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: VS2010/.Net4.0 Global access to a created class

    Actually your TxDataObject class should be in a seperate file from your form class. If you want to make it available to your entire project then you can define the class as static which will allow you to basically work with one instance of the class or you can define it as public, create a public instance in your form1 and then reference that instance in the other places either by passing it as a parameter or by referencing the instance created in form1.
    Always use [code][/code] tags when posting code.

  4. #4
    Join Date
    Feb 2003
    Location
    Ft. Worth Texas
    Posts
    31

    Re: VS2010/.Net4.0 Global access to a created class

    DataMiser,

    Your recommendation of moving to a seperate class file and defining the class as Static is exactly what I needed to do!

    Thanks for your help!

    KKW

  5. #5
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: [RESOLVED] VS2010/.Net4.0 Global access to a created class

    You're welcome.
    Always use [code][/code] tags when posting code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured