Referring to modules from separate classes

i am using the rs232 module.
how do i refer to it in a class other than program so that i can utilize it in my class?

i tried:


but that did not work

this line works in program

```cs]rs232.serialPort.Open();[/code


but not in my class

I would pass a SerialPort object as an input argument in the constructor for my class.

Depending on what you are doing… maybe an event\delegate?

@ mPatterson557 - Not exactly secure but you could simply mark the member as a public static and init it outside of using the gadgeteer designer:

namespace StaticMember
{
    public partial class Program
    {
        public static GTM.GHIElectronics.RS232 SerialObject;

        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            /*******************************************************************************************
            Modules added in the Program.gadgeteer designer view are used by typing 
            their name followed by a period, e.g.  button.  or  camera.
            
            Many modules generate useful events. Type +=<tab><tab> to add a handler to an event, e.g.:
                button.ButtonPressed +=<tab><tab>
            
            If you want to do something periodically, use a GT.Timer and handle its Tick event, e.g.:
                GT.Timer timer = new GT.Timer(1000); // every second (1000ms)
                timer.Tick +=<tab><tab>
                timer.Start();
            *******************************************************************************************/


            // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
            Debug.Print("Program Started");

            SerialObject = new GTM.GHIElectronics.RS232(1);
        }
    }
}

And then access will be as you expected it initially:

namespace StaticMember
{
    class TestAccess
    {
        public void Init()
        {
            Program.SerialObject.serialPort
        }
    }
}

Just remember to watch namespaces :slight_smile:

Why would init out of designer changer how I access it?
How do I drop the program off of the second quoted line?

How would i implement this? Code sample generic is fine or some terms I can do research on thanks.

How would i implement this? What is an event delegate?

Passing as a parameter


public class MyClass{
   void Function(RS232 rs232){
      rs232.BaudRate = 9600;
   }
}

Or use it as a property off your main program.


    public partial class Program
    {
        void ProgramStarted()
        {
            // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
            Debug.Print("Program Started");
        }

        public RS232 RSPort
        {
            get
            {
                return rs232;
            }
            set
            {
                rs232 = value;
            }
        }
    }

      public class MyClass
    {
        void Function()
        {
             Program.RSPort.BaudRate = 9600;
        }
    }

  1. It allows you to set your own properties on the object declaration. The designer will use its own properties and will not accomplish what you want without a reference to either Program or the object itself.

Static property: static modifier - C# Reference - C# | Microsoft Learn

  1. You dont. Program is an object not a namespace.

  2. Passing the object as a parameter will work, however this will consume more memory.

The second example would not work without a reference passed to MyClass

why does it consume more mem?
I thought the objekt was given as referenz and not as copy of itself.

It is, but the reference itself consumes memory, just not as much as the object itself. In most cases, there will be no need to optimize memory consumption to that extent. I only offered it as an option as I do not know the full extent of his project nor the hardware he is using. :slight_smile:

Thanks for the Help. This is what I went with eventually: If glaring faults please let me know.
Implemented in code as:

            public ILogger logger { get; set; }
            public IProgram program { get; set; }
            public virtual bool GetAFI(out byte AFI){
                try{
                    byte[] tid = null;
                    if (RetrySend(GetUIDCommand, 17, ref tid))
                    {
                        AFI = tid[10];
                        return true;
                    }
                }
                catch (Exception exception){
                    logger.LogError("RFID", "GetTID()", exception.Message, exception.StackTrace, exception.InnerException);
                }
                AFI = 0x00;
                return false;
            }

Interface to expose methods:

    public interface IProgram
    {
        bool rs232IsOpen { get; }
        void rs232Open();
        void rs232Close();
        void rs232Send(byte[] bytes);
        byte[] rs232Send(byte[] bytes, int expectedBytes);
    }

Example Method exposed from program:

        private void rs232Open()
        {
            try
            {
                if (!rs232IsOpen)
                {
                    rs232.serialPort.Open();
                    Thread.Sleep(10);
                }
            }
            catch (Exception exception)
            {
                Logger.LogError("rs232.serialport", "Open()", exception.Message, exception.StackTrace, exception.InnerException);
            }
        }

:slight_smile: ;D ;D ;D :dance: :slight_smile: