473,463 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Is there a way to have a Shared instance of a Class in a DLL

JB
Hi All,

I'm building a DLL that can be used by several applications.
I'd like to have one single instance of a Class stored in my DLL that
would be ("shared") by all the applications using that DLL.

I've tried to declare in my DLL a Class with a single "Shared" member,
but each time I reference the DLL from another application, a new
instance is created.

For instance:

'My shared Class (in the DLL)
Public Class CSharedKernel
Public Shared MainKernel As New CKernel
End Class

'The content of the shared class (in the DLL)
Public Class CKernel
Public m_KernelString As String = ""

Sub New()
m_KernelString = "Kernel Object created at " &
Now.ToString("HH:mm:ss")
End Sub
End Class

'When I call the following in 2 separate applications using the DLL
'I get 2 different times telling me it's actually 2 different
'instances of CKernel
MsgBox(CSharedKernel.MainKernel.m_KernelString")

Is this possible to achieve at all in a DLL?
Thanks a lot

Aug 27 '07 #1
6 1404
JB,

That is named a Windows Service.

Cor

"JB" <jb*********@gmail.comschreef in bericht
news:11**********************@r34g2000hsd.googlegr oups.com...
Hi All,

I'm building a DLL that can be used by several applications.
I'd like to have one single instance of a Class stored in my DLL that
would be ("shared") by all the applications using that DLL.

I've tried to declare in my DLL a Class with a single "Shared" member,
but each time I reference the DLL from another application, a new
instance is created.

For instance:

'My shared Class (in the DLL)
Public Class CSharedKernel
Public Shared MainKernel As New CKernel
End Class

'The content of the shared class (in the DLL)
Public Class CKernel
Public m_KernelString As String = ""

Sub New()
m_KernelString = "Kernel Object created at " &
Now.ToString("HH:mm:ss")
End Sub
End Class

'When I call the following in 2 separate applications using the DLL
'I get 2 different times telling me it's actually 2 different
'instances of CKernel
MsgBox(CSharedKernel.MainKernel.m_KernelString")

Is this possible to achieve at all in a DLL?
Thanks a lot
Aug 27 '07 #2
Each application has its own single copy of the class. There is no
way, just using VB apps, to have a global instance across mutiple
applications.

You might be able to do it with COM. A Windows Service would be
another possibility.

On Mon, 27 Aug 2007 07:33:21 -0700, JB <jb*********@gmail.comwrote:
>Hi All,

I'm building a DLL that can be used by several applications.
I'd like to have one single instance of a Class stored in my DLL that
would be ("shared") by all the applications using that DLL.

I've tried to declare in my DLL a Class with a single "Shared" member,
but each time I reference the DLL from another application, a new
instance is created.

For instance:

'My shared Class (in the DLL)
Public Class CSharedKernel
Public Shared MainKernel As New CKernel
End Class

'The content of the shared class (in the DLL)
Public Class CKernel
Public m_KernelString As String = ""

Sub New()
m_KernelString = "Kernel Object created at " &
Now.ToString("HH:mm:ss")
End Sub
End Class

'When I call the following in 2 separate applications using the DLL
'I get 2 different times telling me it's actually 2 different
'instances of CKernel
MsgBox(CSharedKernel.MainKernel.m_KernelString" )

Is this possible to achieve at all in a DLL?
Thanks a lot
Aug 27 '07 #3
> There is no
way, just using VB apps, to have a global instance across mutiple
applications.
Actually there is a way ( both VB and C# support this technique )

You could use a singleton class in a remoting host , this acts exactly how
you describe it
this way you can access the class across application domains and even across
the network / internet

regards

Michel

"Jack Jackson" <ja********@pebbleridge.comschreef in bericht
news:90********************************@4ax.com...
Each application has its own single copy of the class. There is no
way, just using VB apps, to have a global instance across mutiple
applications.

You might be able to do it with COM. A Windows Service would be
another possibility.

On Mon, 27 Aug 2007 07:33:21 -0700, JB <jb*********@gmail.comwrote:
>>Hi All,

I'm building a DLL that can be used by several applications.
I'd like to have one single instance of a Class stored in my DLL that
would be ("shared") by all the applications using that DLL.

I've tried to declare in my DLL a Class with a single "Shared" member,
but each time I reference the DLL from another application, a new
instance is created.

For instance:

'My shared Class (in the DLL)
Public Class CSharedKernel
Public Shared MainKernel As New CKernel
End Class

'The content of the shared class (in the DLL)
Public Class CKernel
Public m_KernelString As String = ""

Sub New()
m_KernelString = "Kernel Object created at " &
Now.ToString("HH:mm:ss")
End Sub
End Class

'When I call the following in 2 separate applications using the DLL
'I get 2 different times telling me it's actually 2 different
'instances of CKernel
MsgBox(CSharedKernel.MainKernel.m_KernelString ")

Is this possible to achieve at all in a DLL?
Thanks a lot

Aug 27 '07 #4
To make it a true singleton add this to the singleton class
#Region " Remoting Override "
''' -----------------------------------------------------------------------------
''' <summary>
''' this method will make sure that the leasetime will be infinite
''' </summary>
''' <returns>Nothing as valid lease point expiration time </returns>
''' <remarks>
''' </remarks>
''' <history>
''' [michel] 6/4/2005 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Overrides Function InitializeLifetimeService() As Object
Return Nothing
End Function
#End Region

If you omit this the singleton will have a lease time of 20 minutes or so

regards

Michel
"Michel Posseth [MCP]" <MS**@posseth.comschreef in bericht
news:uE**************@TK2MSFTNGP02.phx.gbl...
>> There is no
way, just using VB apps, to have a global instance across mutiple
applications.

Actually there is a way ( both VB and C# support this technique )

You could use a singleton class in a remoting host , this acts exactly how
you describe it
this way you can access the class across application domains and even
across the network / internet

regards

Michel

"Jack Jackson" <ja********@pebbleridge.comschreef in bericht
news:90********************************@4ax.com...
>Each application has its own single copy of the class. There is no
way, just using VB apps, to have a global instance across mutiple
applications.

You might be able to do it with COM. A Windows Service would be
another possibility.

On Mon, 27 Aug 2007 07:33:21 -0700, JB <jb*********@gmail.comwrote:
>>>Hi All,

I'm building a DLL that can be used by several applications.
I'd like to have one single instance of a Class stored in my DLL that
would be ("shared") by all the applications using that DLL.

I've tried to declare in my DLL a Class with a single "Shared" member,
but each time I reference the DLL from another application, a new
instance is created.

For instance:

'My shared Class (in the DLL)
Public Class CSharedKernel
Public Shared MainKernel As New CKernel
End Class

'The content of the shared class (in the DLL)
Public Class CKernel
Public m_KernelString As String = ""

Sub New()
m_KernelString = "Kernel Object created at " &
Now.ToString("HH:mm:ss")
End Sub
End Class

'When I call the following in 2 separate applications using the DLL
'I get 2 different times telling me it's actually 2 different
'instances of CKernel
MsgBox(CSharedKernel.MainKernel.m_KernelString" )

Is this possible to achieve at all in a DLL?
Thanks a lot


Aug 27 '07 #5
JB
On 27 Aug, 21:49, "Michel Posseth [MCP]" <M...@posseth.comwrote:
To make it a true singleton add this to the singleton class

#Region " Remoting Override "
''' -----------------------------------------------------------------------------
''' <summary>
''' this method will make sure that the leasetime will be infinite
''' </summary>
''' <returns>Nothing as valid lease point expiration time </returns>
''' <remarks>
''' </remarks>
''' <history>
''' [michel] 6/4/2005 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Overrides Function InitializeLifetimeService() As Object
Return Nothing
End Function
#End Region

If you omit this the singleton will have a lease time of 20 minutes or so

regards

Michel

"Michel Posseth [MCP]" <M...@posseth.comschreef in berichtnews:uE**************@TK2MSFTNGP02.phx.gbl. ..
> There is no
way, just using VB apps, to have a global instance across mutiple
applications.
Actually there is a way ( both VB and C# support this technique )
You could use a singleton class in a remoting host , this acts exactly how
you describe it
this way you can access the class across application domains and even
across the network / internet
regards
Michel
"Jack Jackson" <jacknos...@pebbleridge.comschreef in bericht
news:90********************************@4ax.com...
Each application has its own single copy of the class. There is no
way, just using VB apps, to have a global instance across mutiple
applications.
You might be able to do it with COM. A Windows Service would be
another possibility.
On Mon, 27 Aug 2007 07:33:21 -0700, JB <jb.bross...@gmail.comwrote:
>>Hi All,
>>I'm building a DLL that can be used by several applications.
I'd like to have one single instance of a Class stored in my DLL that
would be ("shared") by all the applications using that DLL.
>>I've tried to declare in my DLL a Class with a single "Shared" member,
but each time I reference the DLL from another application, a new
instance is created.
>>For instance:
>>'My shared Class (in the DLL)
Public Class CSharedKernel
Public Shared MainKernel As New CKernel
End Class
>>'The content of the shared class (in the DLL)
Public Class CKernel
Public m_KernelString As String = ""
> Sub New()
m_KernelString = "Kernel Object created at " &
Now.ToString("HH:mm:ss")
End Sub
End Class
>>'When I call the following in 2 separate applications using the DLL
'I get 2 different times telling me it's actually 2 different
'instances of CKernel
MsgBox(CSharedKernel.MainKernel.m_KernelString ")
>>Is this possible to achieve at all in a DLL?
Thanks a lot
Hi All,

Thanks very much for your quick responses.
After much reading and posting around the subject, I've come to the
conclusion that I'm gonna have to look into remoting.
I was trying to avoid it as much as possible to stick to things I
know, but I guess it's time for me to face the music and learn it.

Thanks again
Regards
JB

Aug 27 '07 #6
JB <jb*********@gmail.comwrote in news:1188225201.620141.158310
@r34g2000hsd.googlegroups.com:
I'm building a DLL that can be used by several applications.
I'd like to have one single instance of a Class stored in my DLL that
would be ("shared") by all the applications using that DLL.
You could create it as a Web Service or expose a single object through
Remoting/WCF.
Aug 28 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: John Brock | last post by:
I have a base class with several derived classes (I'm writing in VB.NET). I want each derived class to have a unique class ID (a String), and I want the derived classes to inherit from the base...
10
by: darrel | last post by:
I'm still trying to sort out in my head the differences between public and shared when referring to declaring properties or variables. This is my understanding: shared - akin to a 'global'...
11
by: tshad | last post by:
I am setting up some of my functions in a class called MyFunctions. I am not clear as to the best time to set a function as Shared and when not to. For example, I have the following bit...
15
by: Rob Nicholson | last post by:
A consequence of the ASP.NET architecture on IIS has just hit home with a big thud. It's to do with shared variables. Consider a module like this: Public Module Functions Public GlobalName As...
33
by: Joe Fallon | last post by:
1. I have a Base class that has a certain amount of functionality. 2. Then I have a CodeSmith generated class that inherits from the Base class and adds functionality. 3. Since I want to be able...
4
by: Chris | last post by:
Hello, I'm just getting started with VB and am new to the group, so please excuse what may seem to be a rudimentary question. I've been writing basic programs and have noticed that the...
8
by: gemel | last post by:
I have been reading sime material in .NET that throws some doubt on my understanding of shared procedures. With regard to object programming I assumed that variables declared within a class were...
4
by: Brett | last post by:
I'm trying to use the F1 function inside of F2 function below. I keep getting the error posted below the code. If I remove the Shared declaration from F2, it works fine. What exactly does the...
11
by: eBob.com | last post by:
I have this nasty problem with Shared methods and what I think of as "global storage" - i.e. storage declared outside of any subroutines or functions. In the simple example below this "global"...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.