Jack said:
I have a specific need to implement a singleton typed dataset and still use
drag and drop binding via project data source.
Specifically, I have a dll assembly that contains the typed dataset. When
I
reference it in my WinForms app, I want all forms and user-controls
referencing the dataset type to point to only one instance at runtime.
Please no guesses! I need answers.
Thanks.
Are you using .NET 2.0 or .NET 1.1? Also are you asking how to implement a
singleton class or a factory class? One of the problems I have seen in .NET
2.0 is that you can not hide the public constructor that the designer
creates for the strongly-typed dataset which is a requirement to implement a
singleton class. Regardless here are the steps to create a singleton class.
(If you are using .NET 2.0, I suggest you do this in a partial class.)
1. IN .NET 1.1, hide the constructor that the designer creates by declaring
it as private.
2. Declare a private static (or shared) member variable.
3. Declare a public static method that returns the DataSet.
4. Implement th static method such that it checks to see if the static
member is null. If the static member is null, then set it to an instance of
the dataset. If it is not null, return the instance that was previously
created.
Here is the sample code.
Public Class MyDataSet
Private Shared m_ds As MyDataSet
Public Shared Function GetInstance As MyDataSet
If m_ds Is Nothing Then
m_ds = New MyDataSet
End If
Return m_ds
End Sub
End Class
You can still use drag and drop binding, but you will have to replace the
code where the designer instantiates the DataSet to call the GetInstance
method.
Regards,
Fritz