Click here to Skip to main content
15,867,991 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Cannot use ref or out parameter 'strClientId' inside an anonymous method,lambda expression or query expression

C#
static BackgroundWorker worker;
        static void Main(string[] args)
        {
            worker = new BackgroundWorker();
            worker.DoWork += worker_DoWork;
            
            worker.RunWorkerAsync();
            Console.ReadLine();
        }
        static void worker_DoWork(object sender,DoWorkEventArgs e)
        {
            string strClientId = "2211";
            Authenticate(ref strClientId);
            //whatever You want the background thread to do...
        }
        static void Authenticate(ref string strClientId)
        {
            Timer timer = new Timer(500);
            //Error in this line 
            //Cannot use ref or out parameter 'strClientId' inside an anonymous method,lambda expression or query expression
            timer.Elapsed += (sender, e) => Authenticates_Timer(sender, e,Func<strclientid>);
            //timer.Elapsed += Authenticates_Timer();
            timer.Start();
        }
        static void Authenticates_Timer(object sender, ElapsedEventArgs e, ref string strClientId)
        {
            //want to use variable here
            strClientId = "";
        }</strclientid>
Posted
Updated 31-Jul-15 19:51pm
v3
Comments
George Swan 1-Aug-15 2:27am    
It seems to me that your BackgroundWorker may finish before the timer event fires so your string variable may not be set. If you really want to keep to the structure in your example, I'd wrap the string inside a Stringbuilder object and pass that into the Authenticates_Timer method.

1 solution

Because you can't do it.
If you get an error message like this, google it - take out anything specific to your code, and google the rest: Cannot use ref or out parameter inside an anonymous method,lambda expression or query expression[^]
Generally, you will find that you aren't the first person to meet it...

If you look at the top hit (SO from 2009): http://stackoverflow.com/questions/1365689/cannot-use-ref-or-out-parameter-in-lambda-expressions[^] it explains why you can't do it. You will have to find some other way - and since I have no idea why you want to do that, I can't help!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900