Sharing Class Instances between Forms
Hi,
I need some help! :)
If I have my main form, and I declare an instance of a class (SerialPort in my case) in the main form, how can I pass this class instance to a sub form?
In my example, I create a serial port object in my main form, then when the user clicks the settings button, I want them to see a new 'COM Settings' form where they can change the values of the object I created in the main form?
I've tried overloading the constructor and passing it that way with nothing but compiler errors, and I'm not convinced that's the right way to do it.
Thanks!
Re: Sharing Class Instances between Forms
Quote:
Originally Posted by BigWinston
I've tried overloading the constructor and passing it that way with nothing but compiler errors, and I'm not convinced that's the right way to do it.
This seems like one correct way to do it. What does your code look like? What errors do you get?
- petter
Re: Sharing Class Instances between Forms
Ok, here's the code
Main Form
Code:
public partial class Form1 : Form
{
private serialCom newSeCom = new serialCom(); /* Create the serial port object */
private static serialSettingsFrm SerialSettingsForm = new serialSettingsFrm(newSeCom);
etc...
Secondary Form
Code:
public partial class serialSettingsFrm : Form
{
public serialSettingsFrm(serialCom newSeCom)
{
InitializeComponent();
}
etc...
serialCom is my serial communication driver class.
The error(s) I get is :
Code:
Error 1 Inconsistent accessibility: parameter type 'SAS_Calibrator.serialCom' is less accessible than method 'SAS_Calibrator.serialSettingsFrm.serialSettingsFrm(SAS_Calibrator.serialCom)' C:\Documents and Settings\Desktop\SAS_Calibrator\SAS_Calibrator\serialSettingsFrm.cs 13 16 SAS_Calibrator
I have tried declaring the class instance creation as public in the main form, but I get a similar error ("...is less accessible..blah...blah...blah")
Any Ideas?
Re: Sharing Class Instances between Forms
It looks like serialCom is an internal class, then the constructor requiring an instance of that class should also be internal.