singleton pattern in vb - vb.net

I am normally a c# programmer but am now working in VB for this one project when I use to set up a singleton class I would follow the Jon Skeet model
public sealed class Singleton
{
static Singleton instance = null;
static readonly object padlock = new object();
Singleton()
{
}
public static Singleton Instance
{
get
{
lock (padlock)
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
}
//Added to illustrate the point
public static void a()
{
}
public void b()
{
}
}
or one of the variations now if I write the statement in c#
Singleton.Instance What procedures is all of the members that are not static, b but not a.
Now when I do the same in VB
Private Shared _instance As StackTracker
Private Shared ReadOnly _lock As Object = New Object()
Private Sub New()
_WorkingStack = New Stack(Of MethodObject)
_HistoryStack = New Queue(Of MethodObject)
End Sub
Public Shared ReadOnly Property Instance() As StackTracker
Get
SyncLock _lock
If (_instance Is Nothing) Then
_instance = New StackTracker()
End If
End SyncLock
Return _instance
End Get
End Property
I get StackTracker.Instance.Instance and it keeps going, while it is not the end of the world it looks bad.
Question is there a way in VB to hide the second instance so the user can not recursively call Instance?

Here's the full code:
Public NotInheritable Class MySingleton
Private Shared ReadOnly _instance As New Lazy(Of MySingleton)(Function() New
MySingleton(), System.Threading.LazyThreadSafetyMode.ExecutionAndPublication)
Private Sub New()
End Sub
Public Shared ReadOnly Property Instance() As MySingleton
Get
Return _instance.Value
End Get
End Property
End Class
Then to use this class, get the instance using:
Dim theSingleton As MySingleton = MySingleton.Instance

The original question was not about how to implement the singleton pattern, but referring to the fact that in C# it's a compiler error to try to access a static member via an instance. In the current VB it's a warning.
Solution:
You can change the project compiler settings to "Treat all warnings as errors", but I don't know any way to explicitly treat just warning 42025 as an error.
That being said, there is also a much simpler way to implement singletons in VB:
public class Singleton
private sub new()
end sub
public shared readonly property Instance as Singleton
get
static INST as Singleton = new Singleton
return INST
end get
end property
end class
This relies on VB thread-safe single initialization of static variables which is a feature not found in C#. The line of code beginning with the word "static" is only evaluated once even if the Instance property is accessed many times from many threads.

This is actually not the proposal put forth by Jon. You implemented the third version referenced in the article on the matter, which he points out doesn't work according to the EMCA spec due to lack of memory barriers.
Rather, you should work with the fifth version, which uses a nested class and performs the assignment of the instance in the declaration of the static field on the nested class.
If you are working in .NET 4.0, then you don't have to do any of this. You can create a static readonly field of type Lazy<T>, passing LazyThreadSafetyMode.ExecutionAndPublication to the constructor (along with your Func<T> to indicate how to create the instance) to guarantee that the value will only be created once.
Then, you expose a property which simply calls the Lazy<T>.Value property to return the lazy-loaded singleton value.

Maybe I'm missing something but I just do some variation on this, depending on what else is going on in the class:
Class MySingleton
'The instance initializes once and persists (provided it's not intentionally destroyed)
Private Shared oInstance As MySingleton = New MySingleton
'A property initialized via the Create method
Public Shared Property SomeProperty() As Object = Nothing
'Constructor cannot be called directly so prevents external instantiation
Private Sub New()
'Nothing to do
End Sub
'The property returns the single instance
Public Shared ReadOnly Property Instance As MySingleton
Get
Return oInstance
End Get
End Property
'The method returns the single instance while also initializing SomeProperty
Public Shared Function Create(
ByVal SomeParam As Object) As MySingleton
_SomeProperty = SomeParam
Return oInstance
End Function
End Class
Obviously, you would usually only provide either the Instance property or the Create method, not both (though you could if you wanted to for some reason).
In its simplest form it's:
Class MySingleton
Private Shared oInstance As MySingleton = New MySingleton
Private Sub New()
End Sub
Public Shared ReadOnly Property Instance As MySingleton
Get
Return oInstance
End Get
End Property
End Class

Related

From A Method With In The Class Return An Instance Of The Class As An Interface Type That The Class Implements

what I'm trying to archive with the code below is to have the GetInstance generic function take in an interface type that SystemVars implements (say IAuthentication) then create an instance of SystemVars and return it as interface type T.
The problem I an having is that no matter what casting method I try I can't find a way to return the new instance of SystemVars as T. The line in the GetInstance method Return <CastingFunction>(New SystemVars,T) always fails to compile with the error message saying Value of type SystemVars cannot be converted to 'T'.
How do I return the instance of the class as the interface type that was passed into T?
Imports System.Drawing
Public Class SystemVars
Implements IAuthentication,
IAuthorization,
IApplicationStarting
Private Sub New()
End Sub
Public Shared Function GetInstance(Of T)() As T
Return DirectCast(New SystemVars, T)
End Function
Public ReadOnly Property Username As String _
Implements IAuthentication.Username,
IAuthorization.Username
Get
Return _userName
End Get
End Property
Public ReadOnly Property Rolls As List(Of String) _
Implements IAuthorization.Rolls
Get
Return _rolls
End Get
End Property
Public ReadOnly Property InstallationId As Guid _
Implements IAuthentication.InstallationId,
IApplicationStarting.InstallationId
Get
Return _installationId
End Get
End Property
Public ReadOnly Property MainWindowStartUpPlacement As Rectangle _
Implements IApplicationStarting.MainWindowStartUpPlacement
Get
Return _mainWindowStartUpPlacement
End Get
End Property
'........
Private Shared _userName As String
Private Shared _rolls As List(Of String)
Private Shared _installationId As Guid
Private Shared _mainWindowStartUpPlacement As Rectangle
End Class
You can make an otherwise illegal cast work by passing through Object.
Public Shared Function GetInstance(Of T)() As T
Return DirectCast(CObj(New SystemVars), T)
End Function
You will get a runtime error if the cast isn't possible; as noted in the comments, this strategy is chucking type safety out the window and basically telling the compiler, "Don't bother me, I know what I'm doing." The runtime will throw an InvalidCastException on failure if you don't test and throw yourself. You can test using Type.IsAssignableFrom if you want to create a more developer-friendly error message; there isn't much context available in the debugger at the point of failure, though it may be pretty obvious if you look up the call stack.
For just three interfaces, it might be better to do three separate specific functions rather than a generic version, especially considering that the functions are necessarily Shared (and thus can't themselves be part of an interface).
You might also consider a design that includes a Dependency Injection container. In this kind of design, there would be a configuration step that would associate the interfaces with SystemVars as the implementation, then the client would ask the container for an instance of the interface and receive a SystemVars object.
The rough way that the three options (the third being to cast the SystemVars object to the requested interface) would look in code is:
'Casting a received object to a requested interface
Dim asInterface = DirectCast(SystemVars.GetInstance(), IAuthorization)
'Using a custom casting function on SystemVars
Dim asInterface = SystemVars.GetInstance(Of IAuthorization)
'Using a DI container
'Behavior if the interface isn't supported depends on the container
Dim asInterface = container.GetInstance(Of IAuthorization)
Note that TryCast could be used instead of DirectCast, in which case the result would be Nothing if the interface isn't supported.

How To Access A Shared Property Of A Class Passed As A Type Parameter

I'm trying to access a shared property of a class passed as a parameter to a type-parametrised procedure. The reason why I'm doing this is so I can embed the various API call endpoints (among other class-specific things) as properties within the class itself. I've read some similar SO posts but nothing is close enough to be sure that it isn’t possible (which I think is likely).
Below is the essence of the structure - there's some pseudo code towards the end:
MustInherit Class BaseClass
Shared Property Endpoint As String
End Class
Class Person
Inherits BaseClass
Property Age As Integer
Property Name As String
Sub New()
_Endpoint = "/GetPerson"
End Sub
End Class
Class Event
Inherits BaseClass
Property When As Date
Property Type As String
Sub New()
_Endpoint = "/GetEvent"
End Sub
End Class
Function Retrieve(T As BaseClass)(Id As String) As T
Dim oResp As HttpResponse = MakeGetCall(T.Endpoint, Id) <- T.Endpoint throws a compile error
Return Deserialize(Of T)(oResp.Content)
End Function
Dim oPerson As Person = Retrieve(Of Person)("123")
Dim oEvent As Event = Retrieve(Of Event)("123")
To my tiny mind, I would have thought that, since T’s base class is BaseClass which contains the property Endpoint, I’d be ok. But seemingly not.
I've tried a fair few things from here on SO and other places to overcome this to no avail. Yes, I realize I could perform some kind of endpoint look-up based on the type of T but the above represents a very clean solution and I’d like to get it to work if possible.
Any ideas?
Assuming you want EndPoint to be different for each subclass, you should use MustOverride instead of Shared...
MustInherit Class BaseClass
Public MustOverride Property EndPoint As String
End Class
Then return a constant in each subclass
Class Person
Inherits BaseClass
Public Overrides Property EndPoint As String
Get
Return "/Person"
End Get
You might want to declare EndPoint as ReadOnly too.
The small limitation is that you'll need an instance of the class to access EndPoint (since it isn't Shared). If you have a parameterless constructor, you could use (New Person).EndPoint where needed.

Null reference exceptions on properties of a newly instantiated class? What's going on here?

I'm toying around with a 3rd party library and something has me absolutely puzzled.
When I instantiate this class, immediately all of the properties of the class throw exceptions before any more code even runs. Why is this happening? It's not just this TypedSegmentPWK class, it's all of the typedsegment classes.. of which there are many.
Simple instantiation fails
Imports OopFactory.X12.Parsing
Imports OopFactory.X12.Parsing.Model
Imports OopFactory.X12.Parsing.Model.Typed
....
Dim test As New TypedSegmentPWK
test.PWK04_EntityIdentifierCode = "blah"
Assigning a value to PWK04_EntityIdentifierCode or any other property of test fails with a null reference exception.
TypedSegmentPWK:
Namespace OopFactory.X12.Parsing.Model.Typed
Public Class TypedSegmentPWK
Inherits TypedSegment
Public Sub New()
Public Property PWK01_ReportTypeCode As String
Public Property PWK02_ReportTransmissionCode As String
Public Property PWK03_ReportCopiesNeeded As Integer?
Public Property PWK04_EntityIdentiferCodeEnum As EntityIdentifierCode
Public Property PWK04_EntityIdentifierCode As String
Public Property PWK05_IdentificationCodeQualifier As String
Public Property PWK05_IdentificationCodeQualifierEnum As IdentificationCodeQualifier
Public Property PWK06_IdentificationCode As String
Public Property PWK07_Description As String
Public Property PWK08_ActionsIndicated As String
Public Property PWK09_RequestCategoryCode As String
End Class
End Namespace
TypedSegment:
Namespace OopFactory.X12.Parsing.Model
Public MustInherit Class TypedSegment
Protected Sub New(segmentId As String)
Public Event Initialized As EventHandler
Public Event Initializing As EventHandler
Protected Overridable Sub OnInitialized(e As EventArgs)
Protected Overridable Sub OnInitializing(e As EventArgs)
End Class
End Namespace
Full source here: https://github.com/KeyMarkInc/OopFactory.X12
All the properties reference _segment defined in TypedSegment, e.g.
public string PWK04_EntityIdentifierCode
{
get { return _segment.GetElement(4); }
set { _segment.SetElement(4, value); }
}
However, the _segment variable is not initialized until TypedSegment.Initialize(Container parent, X12DelimiterSet delimiters) is called...
internal void Initialize(Container parent, X12DelimiterSet delimiters)
{
OnInitializing(new EventArgs());
_segment = new Segment(parent, delimiters, _segmentId);
OnInitialized(new EventArgs());
}
This is an internal method, so presumably something in this framework is supposed to call it, and not you as the user. So, I would guess the answer is that you are using the TypedSegmentPWK class incorrectly, although I don't know what the correct way is.

Using Reflection to Get All Static Properties in a Class As Objects VB.NET

I would like to start that I don't want a to hear about how expensive and terrible reflection is. That won't help—I have a very good reason to use reflection and that's not my question.
Specifically, I have a class within a class that contains several static properties of the same type.
Public Class Foo
Public Class Bar
Public Shared Property prop1 As New CustomClass()
Public Shared Property prop2 As New CustomClass()
Public Shared Property prop3 As New CustomClass()
End Class
End Class
Public Class CustomClass
Public Sub DoStuff()
End Sub
End Class
I'm looking to create a method in Foo that calls DoStuff on each of the properties contained within it. How can I do this? Here's the general idea of what I want to include in Foo, but I obviously can't convert PropertyInfo to CustomClass:
Private Sub Example()
For Each prop As PropertyInfo In GetType(Foo.Bar).GetProperties()
DirectCast(prop, CustomClass).DoStuff()
Next
End Sub
How can I get the static properties and cast them to CustomClass objects?
PropertyInfo represents the type's property get/set method pair. To evaluate the getter you simply call GetValue, like so:
(in C# because I'm a language snob)
foreach( PropertyInfo pi in typeof(Foo.Bar).GetProperties() ) {
// Use null as arguments because it's a static property without an indexer.
Object got = pi.GetValue( null, null );
CustomClass got2 = got as CustomClass;
if( got2 != null ) {
Console.WriteLine( got2.ToString() );
}
}
And to convert Dai's answer to VB because I'm not a language snob:
For Each pi As System.Reflection.PropertyInfo in Foo.Bar.GetType.GetProperties()
' Use nothing as arguments because it's a shared property without an indexer.
Dim got = pi.GetValue(Nothing, Nothing)
Dim got2 as CustomClass = DirectCast(got, CustomClass)
If Not IsNothing(got2) Then Console.WriteLine(got2.toString())
Next
huzzah for less lines and more keystrokes...

Activator.CreateInstance - Explain it so I can understand

So I am looking at some sample code, and I am not sure what to make of this:
Private Shared _instance As PollsProvider = Nothing
Public Shared ReadOnly Property Instance() As PollsProvider
Get
If IsNothing(_instance) Then
_instance = CType(Activator.CreateInstance( _
Type.GetType(Globals.Settings.Polls.ProviderType)), PollsProvider)
End If
Return _instance
End Get
End Property
What is the difference between the above and how I would normally make a singleton:
Private Shared _instance As PollsProvider = Nothing
Public Shared ReadOnly Property Instance() As PollsProvider
Get
If IsNothing(_instance) Then
_instance = New PollsProvider
End If
Return _instance
End Get
End Property
The first code fragment reads the type of PollsProvider to create from config, whereas the second has the type of PollsProvider compiled in. The first fragment therefore allows you to switch in configuration (without a recompile/redeploy) between RealPollsProvider, TestPollsProvider, FiddledByOurEvilPaymastersPollsProvider, etc.
itowlson got it right. I will add it looks like PollsProvider probably a interface or a class that other classes inherit from.