79194533

Date: 2024-11-16 04:43:20
Score: 2.5
Natty:
Report link

I am following @Craig's comment. I just don't feel it's elegant.

So I basically add this function

Protected Sub updateTypeName()
    _TypeName = Me.GetType().Name
End Sub

And then I do

Public Sub New(ByVal ParamArray kwargs As EIP712Type())
    MyBase.New("", Nothing) ' Assuming EIP712Type has a constructor like this
    updateTypeName()
    Dim members As List(Of Tuple(Of String, EIP712Type)) = GetMembers()
    values = New Dictionary(Of String, EIP712Type)

So the TypeName is defined as

Public ReadOnly Property TypeName As String

and it's readonly and can only be modified by descendants of EIP712Type and they can only do so by changing the name to the actual name of the class.

If there is a more elegant solution I would like to know.

The issue is VB should already know the type of the class calling new variable. However, unlike in many of other programming language, VB doesn't have a keyword to refer to that class. Me refer to the object. Python usually have self and cls and that has no equivalent in VB.

I maybe missing something.

Reasons:
  • Blacklisted phrase (1.5): I would like to know
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @Craig's
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Andri Timur