La variabile 'ReadOnly' non può essere la destinazione di un'assegnazione in un'espressione lambda all'interno di un costruttore

Si è fatto riferimento a una variabile ReadOnly dall'interno di un'espressione lambda, ma questo non è consentito. Il codice seguente genera questo errore inviando la variabile ReadOnlym come argomento a un parametro ByRef .

Class Class1  
  
    ReadOnly m As Integer  
  
    Sub New()  
        ' The use of m triggers the error.  
        Dim f = Function() Test(m)  
    End Sub  
  
    Function Test(ByRef n As Integer) As String  
    End Function  
  
End Class  

ID errore: BC36602

Per correggere l'errore

  • Se possibile, modificare il parametro n nella funzione Test in un parametro ByVal , come mostrato nel codice seguente.

    Class Class1Fix1  
    
        ReadOnly m As Integer  
    
        Sub New()  
            Dim f = Function() Test(m)  
        End Sub  
    
        Function Test(ByVal n As Integer) As String  
        End Function  
    
    End Class  
    
  • Assegnare la variabile ReadOnlym a una variabile locale nel costruttore e usare la variabile locale al posto di m, come mostrato nel codice seguente.

    Class Class1Fix2  
         ReadOnly m As Integer  
    
        Sub New()  
            Dim temp = m  
            Dim f = Function() Test(temp)  
        End Sub  
    
        Function Test(ByRef n As Integer) As String  
        End Function  
    
    End Class  
    

Vedi anche