Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to reduce this code using anonymous methods or anonymous types?
    primarykey
    data
    text
    <p>I have a web service and some methods and I do NOT want to use this code below, which works fine, because I intend to use this in a multi-threaded app where static (I have read) is not thread safe.</p> <p>So, what I have done now is simply repeat the code that is in the static method 'returnisTrue()' below, in the body of my web service method "myWebMethod", at points //1 of 3, //2 of 3, and //3 of 3. This works fine but results in bloated code. Is there a more compact way, using functional programming or anonymous methods or what have you that I can use?</p> <p>Note the compiler choked when I tried to create a class inside the method...which would have solved my problem.</p> <p>As I say the code I have now works fine but it is bloated, as I repeat the 'static' method 'returnisTrue' three times.</p> <p>Thank you.</p> <p>EDIT: in response to some questions about making my static method thread safe, which I rather not bother with, but in the interest of getting a solution I include below.</p> <p>All this code is server side in a web service </p> <pre><code>// I cannot unfortunately use this static method below--not thread safe static private bool returnisTrue() { bool isTrue = false; // do a bunch of stuff here return isTrue; } public bool myWebMethod (string XYZ) { //public Class myClassReturnisTrue { … } //will not compile bool isTrueOrNot1 = returnisTrue(); //1 of 3 /// more code here bool isTrueOrNot2 = returnisTrue(); //2 of 3 ///more code here bool isTrueOrNot3 = returnisTrue(); //3 of 3 return isTrueOrNot1 &amp;&amp; isTrueOrNot2 &amp;&amp; isTrueOrNot3; } // here is the static method 'returnisTrue' it looks something like this: static private bool returnIsTrue(string A, string B) { if (A.Length() &lt; B.Length()) { return true; } else return false; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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