|
-
September 22nd, 2011, 07:15 PM
#1
[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!
-
September 23rd, 2011, 10:26 AM
#2
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.
-
September 23rd, 2011, 11:47 AM
#3
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.
-
September 23rd, 2011, 12:51 PM
#4
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
-
September 23rd, 2011, 01:09 PM
#5
Re: [RESOLVED] VS2010/.Net4.0 Global access to a created class
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|