locked
is inside a scope that defines a variable that is used in a lambda or query expression RRS feed

  • Question

  • Hello,

     

    when I use lambda function and goto in same sub or function, I am getting that error message.

    Ok I found explanation on msdn but I didnt understand it exactly. It tells me that

    GoTo

    Compiler doesn't allow to GoTo into scope that contains closure.

    Example 23

       Sub Example()
         GoTo lable1
         While True
           Dim score As Integer = 90
           
     lable1:
     
           Dim results = Aggregate student In Students _
                  Where student.mathScore > score Into Count()
                  
           Exit While
         End While
       End Sub
    

    In my earlier example, in the Lifetime shim section, there is an extra work the compiler has to do here. The closure has to be initialized before the local variable "score" is accessed. Therefore, allowing this will make it difficult for the compiler to maintain the previous task. As a result, the above query statement will cause a compile-time error:

    "error BC36597: 'GoTo lable1' is not valid because 'lable1' is inside a scope that defines a variable that is used in a lambda or query expression."

    here is the url and see the example 23

    http://msdn.microsoft.com/en-us/library/cc441753(VS.90).aspx

     

    If I understand that correctly, there is an ambigous situation while score parameter is between label and goto and It is used after label.

    I had similar situation and I moved my parameter after label. according to the msdn sample if I move score right after label, just like below. It still doesnt work.

    only If I move while after label, compiler accepts. So what is the deal here? I cant use goto at all ?

     

     Sub Example()
         GoTo lable1
         While True        
     lable1:
     
    
     Dim score As Integer = 90
    
    Dim results = Aggregate student In Students _ Where student.mathScore > score Into Count() Exit While End While End Sub


    "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."
    • Moved by liurong luo Tuesday, October 26, 2010 3:31 AM Off-topic here. (From:ADO.NET DataSet)
    Thursday, October 21, 2010 4:22 PM

Answers