Pass arguments in ElapsedEventHandler C++/CLI - timer

How do I pass arguments to my function with ElapsedEventHandler?
NewTimer->Elapsed+=gcnew ElapsedEventHandler(&TimerEvent(String1,String2)); ???
I get this error:
'System::Timers::ElapsedEventHandler' : a delegate constructor expects 2 argument(s)
Any help appreciated.

ElapsedEventHandler defines a delegate that takes two parameters:
The object that raised the event (the timer object).
ElapsedEventArgs, which contains the time that the timer was triggered.
If you need other information in your event handler, store them in fields on your class, and reference them that way.

Related

Calling Apex method with multiple signatures from LWC

I've noticed some interesting behavior in an LWC that I am building and haven't been able to find much info on the cause. Basically I have an Apex method declared with multiple signatures:
// myMethod with 2 param signature
#AuraEnabled
public static void myMethod(String param1, String param2) {
// I expect this method to be invoked when called from the LWC code shown below
....
}
// myMethod with 3 param signature
#AuraEnabled
public static void myMethod(String param1, String param2, Boolean param3) {
// However this method gets invoked instead
....
}
When I attempt to call myMethod from an LWC, and only pass two parameters into the method, I would expect that the 2 param signature method would be invoked, however what happens is that the 3 param signature method is invoked, and the value of null is passed as the third params value.
runJob({param1, param2}).then(value => ... )
Is this expected behavior? When I call the methods through apex, the correct method is invoked by its signature, however this doesn't seem to be the case when calling the method from an LWC.
Is there a way for me to invoke the myMethod Apex method of the correct signature from an LWC?
Edit:
it will be banned at compile time in Summer'22 release. https://help.salesforce.com/s/articleView?id=release-notes.rn_apex_ValidationForAuraEnabledAnnotation.htm&type=5&release=238
Original:
It's worse than you think. runJob({param2, param1}) will work correctly too. "Correctly" meaning their names matter, not the position!
Your stuff is always passed as an object, not a list of parameters. You could have a helper Apex wrapper class and (assuming all fields are #AuraEnabled) it'll map them correctly. If you have list of parameters - well, a kind of unboxing and type matching happens even before your code is called. If you passed "abc" to a Date variable - a JSON deserialization error is thrown even before your code runs, you have no means to catch it.
https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.apex_wire_method
If the Apex method is overloaded, the choice of what method to call is
non-deterministic (effectively random), and the parameters passed may
cause errors now or in the future. Don't overload #AuraEnabled Apex
methods.
So... pick different names? Or funnel them so the 3-param version looks at the last param and calls 2-param one if needed and the business logic allows it. Generally - keep it simple, leave some comments and good unit tests or 2 months from now poor maintenance guy will struggle.
And (if you use the Apex PMD plugin and/or sfdx scanner) functions with long parameter lists are frowned upon anyway: https://pmd.github.io/latest/pmd_rules_apex_design.html#excessiveparameterlist
See also
https://github.com/trailheadapps/lwc-recipes/issues/196
https://salesforce.stackexchange.com/questions/359728/apex-method-overloading-is-not-working-when-called-from-lwc-javascript-controlle

I don't understand this sequenced calling of static methods in Snackbar class. Can someone explain it? Thanks

Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
how does the above code works?
The Snackbar class has a static method make that takes 3 parameters (view, "Replace with your own action", Snackbar.LENGTH_LONG). I don't know what view is but the 2nd is a string and the third is a final variable defined in Snackbar (most probably a long value).
The method make returns a object which has a method called setAction which has two parameters ("Action", null) The first parameter is a String and the 2nd could be any type of object.
The method setAction returns a new object which has a method show() which may return a new value but can also be void.

What does this error actually mean?

So I have seen this error show up, just sometimes, but it is not helpful in describing the actual error which has occured. Nor does it give any clues as to what might cause it to display.
Cannot use modParams with indexes that do not exist.
Can anyone explain more verbosly what this error means, what it relates to (such as a behaviour, component, controller, etc), the most common causes and how to fix it?
To kickstart the investigation, you can find the error here.
https://github.com/cakephp/cakephp/blob/master/lib/Cake/Utility/ObjectCollection.php#L128
Layman's Terms
CakePHP is being told to apply an array of parameters to a collection of objects, such that each particular object can modify the parameters sent on to the next object. There is an error in how CakePHP is being told to do this.
In Depth
Generically, this rises from the CakePHP event publication mechanism. Somewhere in your code is an instance of ObjectCollection, which is being triggered with certain parameters. That is, a method is being called on every object in that collection.
Each callback method is given parameters. Originally the parameters are passed into trigger(). In normal cases (where modParams is false), every callback gets the same parameters. But when modParams is not strictly false, the result of each callback overwrites the parameter indicated by modParams.
So if there are two objects in the collection, modParams is 1, and the params[1] is 'a' initially, then the callback is given the first object with params[1] == a. That callback returns 'b', so when the next callback is called, the second object gets params[1] == b.
The exception raises when the modParams value given does not exist in the originally given params. Eg, if modParams is 2 and params is array (0 => 'a', 1 => 'b'), you'll get this exception.
In your case
Specifically, debugging this has to be done at a low-level because it's a method on a generic class. The backtrace from the Exception should get you bubbled up to a trigger() call on a particular concrete class. That call is being given non-false modParams and a params that doesn't have the given modParams. It could be a code bug in a concrete class extending ObjectCollection, or it could simply be a generic message arising from a method not being given expected arguments.
Have you tried reading the documentation?
/*
* - `modParams` Allows each object the callback gets called on to modify the parameters to the next object.
* Setting modParams to an integer value will allow you to modify the parameter with that index.
* Any non-null value will modify the parameter index indicated.
* Defaults to false.
*/
You did not paste any code, so I guess your 3rd arg of the method contains something wrong.

How do I pass parameters to a callback function for a timer interrupt on Arduino?

I want a timer interrupt to call a function with a set of parameters on an Arduino. I have looked at the Timer1 library, but it seems unable to be used in a way to pass parameters to the callback function.
You can't. You'll have to use some sort of global struct to save your current state. When the callback is used, get the data you need from that struct.
You can wrap your callback into a parameterless function. This wrapper function can pass whatever parameters you want into your function.

Can observable collection be passed as ref parameter?

Can an observable collection be passed as a reference parameter?
I am getting an error that this can't be passed as a reference parameter.
This is the exact error message I am getting:
"A property of indexer may not be passed as an out or ref parameter".
Yes, you can pass an ObservableCollection by reference. However, you can't pass this by reference for any class. (You can for a struct, but please don't.)
If you're having trouble passing a normal variable by reference, please post some code.
EDIT: I've just seen your edit... and the compiler error message is fairly clear. You can't pass properties or indexers by reference. So you can't do:
Foo(ref x.SomeProperty);
but you can do:
var tmp = x.SomeProperty;
Foo(ref tmp);
x.SomeProperty = tmp;

Resources