Is Public Shared Readonly user optimized for VB.Net?

I have such strange behavior in VB.Net. I am stuck. At the moment, I can’t determine if I am missing a concept or is it a mistake.

I have a base class:

Public MustInherit Class BaseType(Of T As BaseType(Of T)) : Implements IEquatable(Of BaseType(Of T)) Protected Sub New(key As Integer, value As List(Of String)) LogManager.GetLogger("INFO").Info("Strings: " & vbTab & value.Count()) If key <> -1 Then enumValues.Add(CType(Me, T)) End If End Sub Protected Shared Function TypeFromStringBase(name As String) As T For Each b As T In enumValues LogManager.GetLogger("DEBUG").Info(b.Names(0)) If b.Names.Contains(name) Then Return b End If Next Return TypeFromStringBase("Keine") End Function End Class 

And the class that inherits it:

 Public Class AnschlussType : Inherits BaseType(Of AnschlussType) : Implements IEquatable(Of AnschlussType) Public Shared ReadOnly Rund As New AnschlussType(1, {"Rund", "1"}) Public Shared ReadOnly Flach As New AnschlussType(2, {"Flach", "2"}) Public Shared ReadOnly Gewinde As New AnschlussType(3, {"Gewinde", "3"}) Public Shared ReadOnly Kein As New AnschlussType(4, {"Kein", "None", "4"}) Private Sub New(key As Integer, names As String()) MyBase.New(key, names.ToList()) End Sub Public Shared Function TypeFromString(name As String) As AnschlussType Return TypeFromStringBase(name) End Function End Class 

Here is the weird part that I don't get. When you first call AnschlussType.TypeFromString("Some String") , VB must create all members of the Public Shared ReadOnly . This results in four calls to BaseType.New . Each of these calls then adds its own type to the enumValues list.

After all these initializations, the AnschlussType.TypeFromString call will finally be made. There, it calls TypeFromStringBase , which TypeFromStringBase over the enumValues list that we populated right before that.

All this works fine in DEBUG mode.

Here is the weird part that I don't get. Now I tried the RELEASE mode. enumValues.Count always remained 0. I assumed this because the logger does not print anything, which means it does not iterate, which means that it is zero. So I did a little research and introduced the registration operator in BaseType.New . And this is NOT recorded at all. This leads me to conclude that New is not running at all.

We emphasize that all this works fine in DEBUG mode and with other subtypes that have Public Shared ReadOnly members in the same material. But this does not work in RELEASE mode.

Does anyone have a hint of what I am missing?

+2
visual-studio
source share
1 answer

If a static constructor exists in the class (Section 10.11), the execution of the initializers of the static field occurs immediately before the execution of this static constructor. Otherwise, the static field initializers are executed at the implementation-dependent time until the first use of the static field of this class.

Assuming VB works like C #, your common (i.e. static) fields are not initialized because you did not use them.

Try creating a generic constructor.

0
source share

All Articles