Jump to content
The simFlight Network Forums

newbie question: passing offsets to other methods


Recommended Posts

Hi,

I apologize for the newbie question.

How can I pass an offset to another method?

In my mainTimer_tick method, I need to check a bunch of offset that are toggles. Rather than write the code for every offset, I want to put the code in a method. The method checks to see if the offset has changed, then sends a message through text to speech via the Tolk library.

There's something simple I'm not understanding about how to pass the offset instance to my method.

Right now, I get this error when building my project:

Error    CS1061    'Offset' does not contain a definition for 'Value' and no accessible extension method 'Value' accepting a first argument of type 'Offset' could be found (are you missing a using directive or an assembly reference?)    tfm    C:\Users\jfayr\source\repos\tfm\MainWindow.xaml.cs    144    Active

In my tick method, I have the following method call:

ReadToggle(AvionicsMaster, "avionics master", "active", "off");

And here's the ReadToggle method:

private void ReadToggle(Offset instrument, string name, string OnMsg, string OffMsg)
        {   
            if (instrument.ValueChanged == true)
            {
                if (instrument.Value == 0)
                {
                    Tolk.Output($"{name} {OffMsg}");
                }
                else
                {
                    Tolk.Output($"{name} {OnMsg}");
                }


            }
        }
 

 

Link to comment
Share on other sites

I think I got it! Looks like I need to specify the type of the offset on my method definition.

private void ReadToggle(Offset<uint> instrument, string name, string OnMsg, string OffMsg)

This would assume that the instruments I need to read are all uint. I think a lot of them are, but some may be Byte. Is there any way of making this function work with multiple offset types?

Link to comment
Share on other sites

Hi Jason,

Using offsets indirectly (e.g. passing them around as parameters) gets a bit difficult.

The DLL does have a base (untyped) 'Offset' class, but it doesn't have a Value property as you found out.

The problem with passing the typed Offsets is that you can only pass one type of Offset into the method, as you've also discovered. Some toggle offsets are bytes, some are shorts, some are int. You could just duplicate the function for each type you want to use. However there is a way to have a single method which is preferable....

Pass the offset as the base class but also pass a value which you can convert to a common type from each of the typed offsets you need to use (byte, int etc).

Something like the following... (as it's a toggle I've chosen bool as the common value type, but you can use the same idea for non-toggle data - you just choose an appropriate type to cast the values to (e.g. double)):

        private void ReadToggle(Offset instrument, bool toggleStateOn, string name, string OnMsg, string OffMsg)
        { 
            if (instrument.ValueChanged)
            {
                if (toggleStateOn)
                {
                    Tolk.Output($"{name} {OnMsg}");
                }
                else
                {
                    Tolk.Output($"{name} {OffMsg}");
                }
            }
        }

Call like this using any numeric data type (offset size):

ReadToggle(AvionicsMaster, AvionicsMaster.Value > 0, "Name", "Msg1", "ms3");

Note that in ReadToggle we have the base Offset type with no Value property. It does have the ValueChanged property however.

When calling the method, we still have the strongly-typed offset so we can still use the Value property there to determine the toggle switch value.

Paul

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use. Guidelines Privacy Policy We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.