CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2005
    Location
    Detroit MI
    Posts
    80

    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!
    Regards,

    Big Winston

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    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

  3. #3
    Join Date
    Mar 2005
    Location
    Detroit MI
    Posts
    80

    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?
    Regards,

    Big Winston

  4. #4
    Join Date
    Apr 2005
    Posts
    576

    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.

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