Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

vb.net - 'ByRef' parameter '<parametername>' cannot be used in a lambda expression

I'm using SharpZipLib to compress files. The library is wrapped in a plugin interface, in a separate DLL. I pass the plugin dll a ByRef parameter to keep track of the compression progress.

SharpZipLib, while compressing, will periodically call a delegate sub passed when launching the compression. I can't figure out how to update the ByRef parameter when the delegate is called. If I try to assign the ByRef variable in the body of a lamba expression, I get a 'ByRef' parameter '<parametername>' cannot be used in a lambda expression error.

Here's my code:

Using InputFile As New IO.FileStream(SourceFile, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
    Using OutputFile As New IO.FileStream(DestFile, IO.FileMode.Create)
        Using GZipStream As New GZipOutputStream(OutputFile)
            Dim Buffer(524228) As Byte
            Dim Handler As New ProgressHandler(Sub(Sender As Object, EventArgs As ProgressEventArgs) Progress += EventArgs.Processed)
            StreamUtils.Copy(InputFile, GZipStream, Buffer, Handler, New TimeSpan(10000000), Nothing, "")
        End Using
    End Using
End Using 

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I know that question is 4 years old but i'm just facing the same problem and i figured it out so i want to share the solution with you.

According to the Microsoft answer on the MSDN page:

You have to assign the ByRef parameter to a local variable, and use the local variable in the lambda expression.

Hope the answer help anyone.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...