2

If I have a class with a shared property, and the properties value is a new object instance created outside of a class procedure (sub/function), can I also set the properties of that shared object outside of a procedure?

Public Class Person

    Private Shared DataItem = New DataItem

    DataItem.Value = 10 ' Assuming Value is a Public Property

End Class

I think in other languages, such as Java, you could create a static block to run and setup static values: static {}

Not sure if you can do that in VB though...

EDIT: Basically the VB equivalent of a static initializer found in Java. Can't seem to find any info on this.

Mayron
  • 2,146
  • 4
  • 25
  • 51

1 Answers1

3

Yes you can. You will need some changes to your code if you want your DataItem variable to be accessible outside of the Person class. You you will need to change Private Shared DataItem to Public Shared DataItem or Friend Shared DataItem. If you want to limit accessibility to reading or writing you can use a method in Person to give access to specific fields. For example:

Public Class Person

    Private Shared DataItem = New DataItem


    Public Sub SetVariable(ByVal value As Int)
        DataItem.Value = value
    End Sub

End Class

If you wanted Shared method on the parent class you can do this:

Public Class Person
    Private Shared DataItem = New DataItem

    Shared Sub New()
        DataItem = New DataItem()
    End Sub
End Class

More detail here, https://msdn.microsoft.com/en-us/library/aa711965(VS.71).aspx. From MSDN:

  1. Shared constructors are run before any instance of a class type is created.
  2. Shared constructors are run before any instance members of a structure type are accessed, or before any constructor of a structure type is explicitly called. Calling the implicit parameter less constructor created for structures will not cause the shared constructor to run.
  3. Shared constructors are run before any of the type's shared members are referenced.
  4. Shared constructors are run before any types that derive from the type are loaded.
  5. A shared constructor will not be run more than once during a single execution of a program.
Ben Hoffman
  • 8,149
  • 8
  • 44
  • 71
  • It only needs to be accessed inside the class by instances of Person. The value needs to be the same across all instances of Person. The problem is that the properties of DataItem need to be set and should not be done inside the Person class functions. I can make it Public but not sure why it is needed. – Mayron May 26 '17 at 11:35
  • I was mainly wondering if there is an equivalent way of setting shared properties outside of a sub/function, like C# and Java can do. I think it is called a static initializer: https://stackoverflow.com/questions/335311/static-initializer-in-java – Mayron May 26 '17 at 11:38
  • 1
    Thank you. I just found this: https://msdn.microsoft.com/en-us/library/aa711965(v=vs.71).aspx Which is exactly what I wanted as no instances of Person need to be created before I can start adding property values onto DataItem. – Mayron May 26 '17 at 11:43