Jason Fayre Posted July 13, 2020 Report Posted July 13, 2020 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}"); } } }
Jason Fayre Posted July 13, 2020 Author Report Posted July 13, 2020 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?
Paul Henty Posted July 13, 2020 Report Posted July 13, 2020 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
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now