Serialize global variables

M

Monty

Hello,

I have a settings class that looks like this:

Imports System.Xml.Serialization
<Serializable()> _
Public Class MySettings

<XmlAttributeAttribute("ItemCategory")> _
Public Shared ItemCategory As Integer

<XmlAttributeAttribute("DefaultItemCategoryID")> _
Public Shared DefaultItemCategoryID As Integer

Public Shared Sub LoadSettings()
ItemCategory = 1042
DefaultItemCategoryID = 1456
End Sub

End Class

As you probably guessed, when I serialize this object to XML, there are no
attributes or values in the XML, just the header and the object name. The
shared variables DO have values. Why is this and how can I serialize these
values?

TIA,

Monty
 
G

Guest

Monty,

XML Serialization does not serialize shared properties/fields.

You could create public non-shared properties as wrappers for the shared
properties/fields.

Kerry Moorman
 
L

Lloyd Sheen

After several experiments with your code I can confirm that it will not take
values. I guess the question is what you are trying to accomplish.

How many instances of this object type do you expect? If only one then you
read it at application startup and either write it each time it changes or
when the application closes. In that case you should not need shared
variables but perhaps a set of module properties which can then return the
values to the application.

Lloyd Sheen
 
M

Monty

Hi Lloyd,

Thanks for trying with my code. I won't have any instances of the object
and will be referencing it's shared values from the far reaches of my web
application. I much prefer to just do something like

X = MySettings.ItemCategory

Than something like

dim o as MySettings = ctype(application("MySettings"), MySettings)
X = o.ItemCategory

I'd love to just serialize/deserialize this object to save it in a
textstream.

Monty
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top