Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You cannot use a <code>ByRef</code> parameter in a lambda because it could be pointing to a location on the stack which no longer exists once the lambda execuetes. All you have to do is use a more "permanent" storage location. You can pass in an object with a property of <code>IEnumerable(Of T)</code> that you can set in order to assign the result.</p> <p>A probably better option is to pass in a delegate (<code>Action&lt;IEnumerable&lt;T&gt;&gt;</code>) that accepts the result and performs whatever action the caller requires with the result. Here's an example in C#:</p> <pre><code>void LoadData&lt;T&gt;(ObjectQuery&lt;T&gt; query, Action&lt;IEnumerable&lt;T&gt;&gt; action) { if (Connection.State == ConnectionState.Open) action(query.ToArray()); else { // create a lambda to handle the next state change StateChangeEventHandler lambda = null; lambda = (sender, e) =&gt; { // only perform our action on transition to Open state if (Connection.State == ConnectionState.Open) { // unsubscribe when we're done Connection.StateChange -= lambda; action(query.ToArray()); } } // subscribe to connection state changes Connection.StateChange += lambda; } } </code></pre> <p>And you would invoke <code>LoadData</code> like this:</p> <pre><code>LoadData(query, results =&gt; listBox.DataSource = results); </code></pre> <p>Note the nuances of my implementation. For example, it does not call itself within the event handler because that will cause it to resubscribe to the event if the handler is ever called with a state other than <code>Open</code>. It also unsubscribes from the event once the connection opens. I'm not sure how this would translate to VB, but in C# this is a 3-step process. First you must declare a variable to hold the lambda and set its value to null. Then you create the lambda, which can now reference itself to unsubscribe. And finally you can use the lambda to subscribe to the event.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload