Hello, I have a singleton settings class (.Net 2.0 framework) that I
serialize/deserialize to XML. On my settings class is a shared list of
integers. If I have two numbers in my list and I deserialize my class
successive times, the count of integers in my list grows by 2 each time when
I would expect it to remain at 2. When I run this code (below) and click the
button multiple times, my immediate window shows the following results:
2
4
6
8
10
Can anyone tell me what's going on here? TIA.
Imports System.Xml.Serialization
<Serializable()_
Public Class cMySettings
Public Shared UserIDs As List(Of Integer)
Public Property zUserIDs() As List(Of Integer)
Get
Return UserIDs
End Get
Set(ByVal value As List(Of Integer))
UserIDs = value
End Set
End Property
End Class
Public Class Form1
Private Const sXML As String = _
"<?xml version=""1.0"" encoding=""utf-16""?<cMySettings" & _
"<zUserIDs><int>1</int><int>2</int></zUserIDs></cMySettings>"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim myStream As New System.IO.StringReader(sXML)
Dim serializer As New
System.Xml.Serialization.XmlSerializer(GetType(cMy Settings))
serializer.Deserialize(myStream)
Debug.Print(cMySettings.UserIDs.Count)
End Sub
End Class 6 1196
On May 12, 7:09 am, "Monty" <mo...@community.nospamwrote:
Hello, I have a singleton settings class (.Net 2.0 framework) that I
serialize/deserialize to XML. On my settings class is a shared list of
integers. If I have two numbers in my list and I deserialize my class
successive times, the count of integers in my list grows by 2 each time when
I would expect it to remain at 2. When I run this code (below) and click the
button multiple times, my immediate window shows the following results:
2
4
6
8
10
Can anyone tell me what's going on here? TIA.
Imports System.Xml.Serialization
<Serializable()_
Public Class cMySettings
Public Shared UserIDs As List(Of Integer)
Public Property zUserIDs() As List(Of Integer)
Get
Return UserIDs
End Get
Set(ByVal value As List(Of Integer))
UserIDs = value
End Set
End Property
End Class
Public Class Form1
Private Const sXML As String = _
"<?xml version=""1.0"" encoding=""utf-16""?<cMySettings" & _
"<zUserIDs><int>1</int><int>2</int></zUserIDs></cMySettings>"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim myStream As New System.IO.StringReader(sXML)
Dim serializer As New
System.Xml.Serialization.XmlSerializer(GetType(cMy Settings))
serializer.Deserialize(myStream)
Debug.Print(cMySettings.UserIDs.Count)
End Sub
End Class
Quick guess here - I didn't run your code.
You have UserIds as a shared field, so every instance you create is
going to adds those ids to the shared field. And since the shared
field won't "die" until the program does, you will continue to get
higher and higher number of UserIds. You should either pull the shared
specifier (if they don't need shared across instances) or implement
IDisposable and clean up the shared property when the instance dies.
Remember however that for the IDisposable strategy to work, you'll
need to explicitly call Dispose since it's a crapshoot at best trying
to let the GC does this for you (you'll never know when the objects
are removed). You best best (imo) is to just pull the shared specifier
if at all possible.
Thanks,
Seth Rowe [MVP]
On 12 Mag, 14:49, rowe_newsgroups <rowe_em...@yahoo.comwrote:
On May 12, 7:09 am, "Monty" <mo...@community.nospamwrote:
Hello, I have a singleton settings class (.Net 2.0 framework) that I
serialize/deserialize to XML. On my settings class is a shared list of
integers. If I have two numbers in my list and I deserialize my class
successive times, the count of integers in my list grows by 2 each time when
I would expect it to remain at 2. When I run this code (below) and clickthe
button multiple times, my immediate window shows the following results:
2
4
6
8
10
Can anyone tell me what's going on here? TIA.
Imports System.Xml.Serialization
<Serializable()_
Public Class cMySettings
* * Public Shared UserIDs As List(Of Integer)
* * Public Property zUserIDs() As List(Of Integer)
* * * * Get
* * * * * * Return UserIDs
* * * * End Get
* * * * Set(ByVal value As List(Of Integer))
* * * * * * UserIDs = value
* * * * End Set
* * End Property
End Class
Public Class Form1
* * Private Const sXML As String = _
* * * * "<?xml version=""1.0"" encoding=""utf-16""?<cMySettings" & _
* * * * "<zUserIDs><int>1</int><int>2</int></zUserIDs></cMySettings>"
* * Private Sub Button1_Click(ByVal sender As System.Object, ByVal eAs
System.EventArgs) Handles Button1.Click
* * * * Dim myStream As New System.IO.StringReader(sXML)
* * * * Dim serializer As New
System.Xml.Serialization.XmlSerializer(GetType(cMy Settings))
* * * * serializer.Deserialize(myStream)
* * * * Debug.Print(cMySettings.UserIDs.Count)
* * End Sub
End Class
Quick guess here - I didn't run your code.
You have UserIds as a shared field, so every instance you create is
going to adds those ids to the shared field. And since the shared
field won't "die" until the program does, you will continue to get
higher and higher number of UserIds. You should either pull the shared
specifier (if they don't need shared across instances) or implement
IDisposable and clean up the shared property when the instance dies.
Remember however that for the IDisposable strategy to work, you'll
need to explicitly call Dispose since it's a crapshoot at best trying
to let the GC does this for you (you'll never know when the objects
are removed). You best best (imo) is to just pull the shared specifier
if at all possible.
Thanks,
Seth Rowe [MVP]- Nascondi testo tra virgolette -
- Mostra testo tra virgolette -
probably, if you need the shared, you could perhaps:
<Serializable()Public Class cMySettings
Sub New()
UserIDs.Clear()
End Sub
Public Shared UserIDs As New List(Of Integer)
Public Property zUserIDs() As List(Of Integer)
Get
Return UserIDs
End Get
Set(ByVal value As List(Of Integer))
UserIDs = value
End Set
End Property
End Class
-P
Thanks Seth, but I guess I don't understand why it would ~add~ to the list
rather than ~replace~ it each time I deserialize my settings? For instance,
if I add a "Name" string property to my singleton (full code below) and
output the name each time along with the count, I get this:
2
Bubba
4
Bubba
6
Bubba
8
Bubba
But given the way the list behaves, why wouldn't I get this?
2
Bubba
4
BubbaBubba
6
BubbaBubbaBubba
8
BubbaBubbaBubbaBubba
[Updated code:]
Imports System.Xml.Serialization
<Serializable()_
Public Class cMySettings
Public Shared UserIDs As List(Of Integer)
Public Shared Name As String
Public Property zName() As String
Get
Return Name
End Get
Set(ByVal value As String)
Name = value
End Set
End Property
Public Property zUserIDs() As List(Of Integer)
Get
Return UserIDs
End Get
Set(ByVal value As List(Of Integer))
UserIDs = value
End Set
End Property
End Class
Public Class cMyEmp
Private _Name As String
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
End Class
Public Class Form1
Private Const sXML As String = _
"<?xml version=""1.0"" encoding=""utf-16""?<cMySettings" & _
"<zUserIDs><int>1</int><int>2</int></zUserIDs>" & _
"<zName>Bubba</zName>" & _
"</cMySettings>"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim myStream As New System.IO.StringReader(sXML)
Dim serializer As New XmlSerializer(GetType(cMySettings))
serializer.Deserialize(myStream)
Debug.Print(cMySettings.UserIDs.Count)
Debug.Print(cMySettings.Name)
End Sub
End Class
That works, grazie!
"pamela fluente" <pa***********@libero.itwrote in message
Monty wrote:
Thanks Seth, but I guess I don't understand why it would ~add~ to the
list rather than ~replace~ it each time I deserialize my settings?
For instance, if I add a "Name" string property to my singleton (full
code below) and output the name each time along with the count, I get
this:
For a list, deserializing will *not* create a new list, then Set the list
property. It will Get the existing list, then add the stored items to the list.
It expects the newly created object to have inititialized a new empty list.
For a string, which is a single value type, not a list or collection, it will
assign the stored value to the property, not append it.
On May 12, 9:17 am, pamela fluente <pamelaflue...@libero.itwrote:
On 12 Mag, 14:49, rowe_newsgroups <rowe_em...@yahoo.comwrote:
On May 12, 7:09 am, "Monty" <mo...@community.nospamwrote:
Hello, I have a singleton settings class (.Net 2.0 framework) that I
serialize/deserialize to XML. On my settings class is a shared list of
integers. If I have two numbers in my list and I deserialize my class
successive times, the count of integers in my list grows by 2 each time when
I would expect it to remain at 2. When I run this code (below) and click the
button multiple times, my immediate window shows the following results:
2
4
6
8
10
Can anyone tell me what's going on here? TIA.
Imports System.Xml.Serialization
<Serializable()_
Public Class cMySettings
Public Shared UserIDs As List(Of Integer)
Public Property zUserIDs() As List(Of Integer)
Get
Return UserIDs
End Get
Set(ByVal value As List(Of Integer))
UserIDs = value
End Set
End Property
End Class
Public Class Form1
Private Const sXML As String = _
"<?xml version=""1.0"" encoding=""utf-16""?<cMySettings" & _
"<zUserIDs><int>1</int><int>2</int></zUserIDs></cMySettings>"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim myStream As New System.IO.StringReader(sXML)
Dim serializer As New
System.Xml.Serialization.XmlSerializer(GetType(cMy Settings))
serializer.Deserialize(myStream)
Debug.Print(cMySettings.UserIDs.Count)
End Sub
End Class
Quick guess here - I didn't run your code.
You have UserIds as a shared field, so every instance you create is
going to adds those ids to the shared field. And since the shared
field won't "die" until the program does, you will continue to get
higher and higher number of UserIds. You should either pull the shared
specifier (if they don't need shared across instances) or implement
IDisposable and clean up the shared property when the instance dies.
Remember however that for the IDisposable strategy to work, you'll
need to explicitly call Dispose since it's a crapshoot at best trying
to let the GC does this for you (you'll never know when the objects
are removed). You best best (imo) is to just pull the shared specifier
if at all possible.
Thanks,
Seth Rowe [MVP]- Nascondi testo tra virgolette -
- Mostra testo tra virgolette -
probably, if you need the shared, you could perhaps:
<Serializable()Public Class cMySettings
Sub New()
UserIDs.Clear()
End Sub
Public Shared UserIDs As New List(Of Integer)
Public Property zUserIDs() As List(Of Integer)
Get
Return UserIDs
End Get
Set(ByVal value As List(Of Integer))
UserIDs = value
End Set
End Property
End Class
-P
One nasty side effect of this is that if you create back to back
cMySettings classes, both will show the most recent items, losing
whatever was added by the first instance. You mention "singleton" in
your subject so hopefully you just haven't added in the singleton
pattern yet so once you do that it should take care of the problem.
Though if you implement singleton, I don't see a reason for the shared
variable.....
Thanks,
Seth Rowe [MVP] This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Tim Clacy |
last post by:
Is there such a thing as a Singleton template that actually saves
programming effort?
Is it possible to actually use a template to make an arbitrary class a
singleton without having to:
a)...
|
by: E. Robert Tisdale |
last post by:
Could somebody please help me with the definition of a singleton?
> cat singleton.cc
class {
private:
// representation
int A;
int B;
public:
//functions
|
by: Jim Strathmeyer |
last post by:
So I'm trying to implement a singleton template class, but I'm getting a
confusing 'undefined reference' when it tries to link. Here's the code
and g++'s output. Any help?
// singleton.h
...
|
by: Alicia Roberts |
last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the
singleton pattern uses a private constructor which in turn reduces
extendability, if you make the Singleton Polymorphic...
|
by: Ethan |
last post by:
Hi,
I have a class defined as a "Singleton" (Design Pattern). The codes are
attached below.
My questions are:
1. Does it has mem leak? If no, when did the destructor called? If yes, how
can I...
|
by: Michael M. |
last post by:
How to find the longst element list of lists?
I think, there should be an easier way then this:
s1 =
s2 =
s3 =
if len(s1) >= len(s2) and len(s1) >= len(s3):
sx1=s1 ## s1 ist längster
|
by: =?Utf-8?B?RmFicmljaW8gRmVycmVpcmE=?= |
last post by:
Hi,
I'm facing an odd problem when consuming some web services in .Net.
After call a web method, the response SOAP envelope isn't being deserialized
properly. In fact, all href tags are not...
|
by: weaknessforcats |
last post by:
Design Pattern: The Singleton
Overview
Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
|
by: desktop |
last post by:
If I have a sorted std::list with 1.000.000 elements it takes 1.000.000
operations to find element with value = 1.000.000 (need to iterator
through the whole list).
In comparison, if I have a...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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...
|
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...
| |