ProfK ProfK - 1 month ago 10
Vb.net Question

What is wrong with this VB.NET extension method on SecureString?

When I try and compile, the following method results in the error, "BC30491 Expression does not produce a value":

<Extension()>
Public Sub FromClearString(ByRef secureString As SecureString, clearString As String)

clearString.ToList().ForEach(Function(c) secureString.AppendChar(c))
secureString.MakeReadOnly()

End Sub


It seems like a perfectly normal extension to me, but I don't work with VB very often. What could be wrong with it?

Answer
clearString.ToList().ForEach('---> Function(c) '<--- secureString.AppendChar(c))

ForEach signature is Action(Of T), and .AppendChar(c) doesn't return a thing

Correct way:

clearString.ToList().ForEach(Sub(c) secureString.AppendChar(c))
Comments