creating static classes? 
Author Message
 creating static classes?

I want to create a DB class that can service my BO hierachy.
In order to implement transaction processing I will need my various
objects sharing the same DB connection.
How do I declare a class as static (the compiler doesnt sem to like
it)?
I suppose I should also return my DB object in the constructor, but
the compiler will not let me return anything via the new method.

any help would be great.

cheers,
geoff



Sun, 23 May 2004 12:41:55 GMT  
 creating static classes?
Hello Geoff,

Quote:
> I want to create a DB class that can service my BO hierachy.
> In order to implement transaction processing I will need my various
> objects sharing the same DB connection.
> How do I declare a class as static (the compiler doesnt sem to like
> it)?
> I suppose I should also return my DB object in the constructor, but
> the compiler will not let me return anything via the new method.

    Make the class 'Public' and the constructor 'Shared'.  Example:

Public Class MySharedClass

    Private Shared mHello As String

    Shared Sub New()

        mHello = "Hello there!"
    End Sub

    Public Shared Property Hello() As String
        Get
            Return mHello
        End Get
        Set(ByVal Value As String)
            mHello = Value
        End Set
    End Property
End Class

    You can then use this class without instantiating it.  Example:

MessageBox.Show(MySharedClass.Hello)    'Will display 'Hello there!'

    Your second option is to place a reference to your connection in a
'Module'.  Your third option would be to wrap your connection in a
singleton object.  This would give you global access to your connection
and ensure that only one instance of the connection can exist at any
given time.

Hope this helps,
Len



Sun, 23 May 2004 16:11:33 GMT  
 creating static classes?
Any chance of an example of the Singleton wrapper - a single instance of a
data access class on a machine would be preferable to multiple instances for
multiple applications.


Quote:
> Hello Geoff,

> > I want to create a DB class that can service my BO hierachy.
> > In order to implement transaction processing I will need my various
> > objects sharing the same DB connection.
> > How do I declare a class as static (the compiler doesnt sem to like
> > it)?
> > I suppose I should also return my DB object in the constructor, but
> > the compiler will not let me return anything via the new method.

>     Make the class 'Public' and the constructor 'Shared'.  Example:

> Public Class MySharedClass

>     Private Shared mHello As String

>     Shared Sub New()

>         mHello = "Hello there!"
>     End Sub

>     Public Shared Property Hello() As String
>         Get
>             Return mHello
>         End Get
>         Set(ByVal Value As String)
>             mHello = Value
>         End Set
>     End Property
> End Class

>     You can then use this class without instantiating it.  Example:

> MessageBox.Show(MySharedClass.Hello)    'Will display 'Hello there!'

>     Your second option is to place a reference to your connection in a
> 'Module'.  Your third option would be to wrap your connection in a
> singleton object.  This would give you global access to your connection
> and ensure that only one instance of the connection can exist at any
> given time.

> Hope this helps,
> Len



Tue, 25 May 2004 05:05:33 GMT  
 creating static classes?
Hello Martin,

Quote:
> Any chance of an example of the Singleton wrapper - a single
> instance of a data access class on a machine would be preferable
> to multiple instances for multiple applications.

    Here is a very basic example, with some comments at the end of the
message:

<SampleDB.vb>
Imports System.Data

Public Class SampleDB

    Private Shared mMe                  As SampleDB
    Private Shared mConn                As IDbConnection

    Private Sub New(ByVal connect As String)

        mConn = New OleDb.OleDbConnection(connect)
    End Sub

    Public ReadOnly Property Connection() As IDbConnection
        Get
            Return mConn
        End Get
    End Property

    Public Shared Sub Create(ByVal connect As String)

        If IsNothing(mMe) Then
            mMe = New SampleDB(connect)
        Else
            Throw New ApplicationException("Object can only be created
once!")
        End If
    End Sub
    Public Shared Function GetInstance() As SampleDB

        If Not IsNothing(mMe) Then
            Return mMe
        Else
            Throw New ApplicationException("Object has not been created
yet!")
        End If
    End Function
End Class
</SampleDB.vb>

    Here is a sample app that uses the above class:

<WindowsApplication1>
    Private Sub Form1_Load(ByVal sender As System.Object, _
                        ByVal e As System.EventArgs) Handles MyBase.Load

        Dim ConnectString               As String

        ConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                        "Data Source=C:\MyFolder\MyDb.mdb;"

        SampleDB.Create(ConnectString)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) _
                              Handles Button1.Click

        Dim db                    As SampleDB

        db = SampleDB.GetInstance()

    End Sub
</WindowsApplication1>

    You will notice that there is no global reference to SampleDB, yet
is was nevertheless accessible from the Button1_Click event.  You will
also notice that the SampleDB constructor is private, so it can only be
accessed from one of its own (shared) methods.  The SampleDB.Create
method will take care of the initial creation, and throw an exception in
subsequent calls (thereby ensuring that one a single instance can
exist).

Hope this helps,
Len



Thu, 27 May 2004 01:25:46 GMT  
 creating static classes?
Sorry, I misunderstood. I am already doing this.

However, can this be used in an EXE/DLL situation - I currently have an
application (EXE) calling a DLL, both written in C# but I do not seem to be
able to access the static class (In the DLL) from the Application.

I used to be able to do it in VB6 using GlobalMultiuse instancing but I have
yet to discover the C# method.


Quote:
> Hello Martin,

> > Any chance of an example of the Singleton wrapper - a single
> > instance of a data access class on a machine would be preferable
> > to multiple instances for multiple applications.

>     Here is a very basic example, with some comments at the end of the
> message:

> <SampleDB.vb>
> Imports System.Data

> Public Class SampleDB

>     Private Shared mMe                  As SampleDB
>     Private Shared mConn                As IDbConnection

>     Private Sub New(ByVal connect As String)

>         mConn = New OleDb.OleDbConnection(connect)
>     End Sub

>     Public ReadOnly Property Connection() As IDbConnection
>         Get
>             Return mConn
>         End Get
>     End Property

>     Public Shared Sub Create(ByVal connect As String)

>         If IsNothing(mMe) Then
>             mMe = New SampleDB(connect)
>         Else
>             Throw New ApplicationException("Object can only be created
> once!")
>         End If
>     End Sub
>     Public Shared Function GetInstance() As SampleDB

>         If Not IsNothing(mMe) Then
>             Return mMe
>         Else
>             Throw New ApplicationException("Object has not been created
> yet!")
>         End If
>     End Function
> End Class
> </SampleDB.vb>

>     Here is a sample app that uses the above class:

> <WindowsApplication1>
>     Private Sub Form1_Load(ByVal sender As System.Object, _
>                         ByVal e As System.EventArgs) Handles MyBase.Load

>         Dim ConnectString               As String

>         ConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
>                         "Data Source=C:\MyFolder\MyDb.mdb;"

>         SampleDB.Create(ConnectString)
>     End Sub

>     Private Sub Button1_Click(ByVal sender As System.Object, _
>                               ByVal e As System.EventArgs) _
>                               Handles Button1.Click

>         Dim db                    As SampleDB

>         db = SampleDB.GetInstance()

>     End Sub
> </WindowsApplication1>

>     You will notice that there is no global reference to SampleDB, yet
> is was nevertheless accessible from the Button1_Click event.  You will
> also notice that the SampleDB constructor is private, so it can only be
> accessed from one of its own (shared) methods.  The SampleDB.Create
> method will take care of the initial creation, and throw an exception in
> subsequent calls (thereby ensuring that one a single instance can
> exist).

> Hope this helps,
> Len



Thu, 27 May 2004 06:08:57 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Static class attributes, how to create them.

2. static classes and methods

3. Static Class and Variable

4. Static class data

5. Static class module in VBA

6. Class Side (Static) functions

7. Static class membervariables?

8. C++ static class property possible in VB ?

9. Static class members

10. static class data in ActiveX DLLs?

11. Static class membervariables?

12. What does Static mean in class context?

 

 
Powered by phpBB® Forum Software