Create an assignment operator that accepts as const value: CLOSED
-
Sorry, this always seems to happen after I post but I think I get it now. I'm trying to assign a value to a class. It compiles if i do this: Private Sub ab(Optional ByVal val As EncryptKey = Nothing) end Sub I was confused by this: Private Sub ab(Optional ByVal val As String = "") end Sub I don't know how String is defined to be able to accept this but what I'm doing won't work. Sorry again for the ugly post. Sorry about the subject line; didn't know how to describe my problem. Through searching around the internet i've come up with this class to be able to process a string value. Class EncryptKey Private _string As String = String.Empty Public Sub New(ByVal value As String) ' Remove white space Me._string = value.Trim() End Sub Public Shared Widening Operator CType(ByVal value As String) As EncryptKey Return New EncryptKey(value) End Operator Public Overrides Function ToString() As String Return _string End Function Public Function Length() As Integer Return Me._string.Length End Function Public Function IsEmpty() As Boolean Return (_string.Length = 0) End Function Public Shared Operator +(ByVal s1 As EncryptKey, ByVal s2 As EncryptKey) As EncryptKey ' Concat and remove white space Dim temp As String = (s1._string + s2._string).Trim() Return New EncryptKey(temp) End Operator End Class The class is working except for this case: Private Sub ab(Optional ByVal val As EncryptKey = "") ' Results in this error for the Optional assigment: 'error BC30060: Conversion from 'String' to 'EncryptKey' cannot occur in a constant expression. End Sub Is there a way to make the class able to accept this assignment as an Optional parameter? Thank you