writing an objectfactory

IfYouSaySo

Well-known member
Joined
Jan 4, 2006
Messages
102
Location
Hermosa Beach, CA
Programming Experience
3-5
So I'm trying to write an object factory, and I've got the code completed, based on how I would have written it in C++. It doesn't quite work. Here's a rundown of some classes and how they work.

The class factory object has a dictionary data member, and two member functions:

VB.NET:
class ObjectFactory
' Add name to the dictionary, where MyDelegate is the value
Sub Register(ByVal name As String, Byval x As MyDelegate)
' Do a dictionary lookup on name, and return the object constructed by the call to the delegate
Function Create(ByVal name As String) As IMyObject
End Class

Next I have my object, and it will look something like this:

VB.NET:
class MyObjectA
    Implements IMyObject
    Public Shared Function Create() As IMyObject
        Dim x As IMyObject = New MyObjectA()
        return x
    End Function
 
    Private Class Registrar
        Public Sub New()
            ObjectFactory.Instance.Register("MyObjectA", MyObjectA.Create)
        End Sub
    End Class
 
    Private Shared theRegistrar As New Registrar()
End Class

So hopefully you can see that I'm trying to take advantage of a static (shared) member variable to cause object registration at DLL load time. And of course, it doesn't work--when main calls ObjectFactory.Instance.Create("MyObjectA") an exception is thrown because the item isn't in the dictionary. My hypothesis is that a shared member variable isn't actually loaded (i.e. new isn't called against it) until the first time that an object of that type is constructed. Which would be different than c++.

So anyway, does anyone know what's going on here, and also how do I get dll load time initialization of an object? Is there some attribute, or is this not possible? I'm looking for a solution that hides the registrar class and it's instances from clients of the DLL.
 
Shared members doesn't evaluate to class instantiation and isn't qualified by any instance of the class. Here is a sample class:
VB.NET:
Class test1
    Private Shared instance As New Dictionary(Of String, Integer)
 
    Public Shared Sub method(ByVal name, ByVal value)
        If Not instance.ContainsKey(name) Then instance.Add(name, value)
    End Sub
 
    Public Shared ReadOnly Property item(ByVal name As String) As Integer
        Get
            If Not instance.ContainsKey(name) Then Return -1
            Return instance(name)
        End Get
    End Property
 
End Class
Here is the tests where you see how Shared works:
VB.NET:
Private Sub frmDiv_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
 
    MsgBox(test1.item("testname")) [COLOR=darkgreen]'returns -1[/COLOR]
    test1.method("testname", 123) [COLOR=darkgreen]'add one item/value[/COLOR]
    MsgBox(test1.item("testname")) [COLOR=darkgreen]'returns 123[/COLOR]
    MsgBox(test1.item("testnameX")) [COLOR=darkgreen]'returns -1[/COLOR]
    Dim x As New test1
    Dim y As New test1
    MsgBox(x.item("testname")) [COLOR=darkgreen]'x is ignored, returns 123[/COLOR]
    MsgBox(y.item("testname")) [COLOR=darkgreen]'y is ignored, returns 123[/COLOR]
 
End Sub
You actually get this error on x/y, but this was rather a point about Shared members/instances.
Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated.
I'm not quite seeing your code, but perhaps all you lacked was the ContainsKey lookup? (see class method example)
 
Back
Top