ProfK ProfK - 1 year ago 93 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":

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

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

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 Source
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))