Persist Variables

M

Matthew

I'm a bit new to VB, so please be understanding.

I would like to have several variables that are remembered even when the
user closes the program and re-opens it.
The following seems to describe what I want:
http://msdn.microsoft.com/library/d...kpersistinginformationinprojectssolutions.asp

However, when I tried copying and pasting the example code to my form1.vb I
get an error "Type 'Solution' not defined".
I have VB.NET 2003.

First of all, is there an easier way to save the variables? If not, does
anybody know how I can get this code to work?

Thanks in advance,

Matthew
 
C

CJ Taylor

You could look into Serialization.

i.e build a class that stores your variables. Create an instance at program
startup, persist it through a module, and when your program starts up, add a
handler to the applicaiton's exit event, and when it exists, use a binary
formatter to save the class to disk.. on startup again, deserialize that
file, variables ready to go..
 
M

Matthew

You could look into Serialization.

That's exactly what I needed! I just didn't know what it was called :'-(
Thanks for pointing me in the right direction.
i.e build a class that stores your variables. Create an instance at
program startup, persist it through a module, and when your program starts
up, add a handler to the applicaiton's exit event, and when it exists, use
a binary formatter to save the class to disk.. on startup again,
deserialize that file, variables ready to go..

When I deserialize my file, do I have to go through and set each one
individually like so:
Label1.ForeColor = System.Drawing.ColorTranslator.FromOle(mySettings.color)

Label1.Font.Name = mySettings.thisfont.name

Label1.Font.Size = mySettings.thisfont.size


Or can the class elements be associated with stuff in my form somehow?

Matthew

P.S. My current code looks like this:

'form1.vb contains:
Dim mySettings As Settings = New Settings
mySettings.color = System.Drawing.ColorTranslator.ToOle(Label1.ForeColor)

mySettings.thisfont.name = Label1.Font.Name

mySettings.thisfont.size = Label1.Font.Size



mySettings.Save("settings.xml", mySettings)



'class1.vb contains:

Imports System.Xml
Imports System.Xml.Schema
Imports System.Xml.Serialization
Imports System.IO

<XmlRoot("config")> Public Class Settings
Private _Name As String
Private _Color As String
Private _Font As thisFont = New thisFont

Public Property name() As String
Get
Return _Name
End Get
Set(ByVal Value As String)
_Name = Value
End Set
End Property
Public Property color() As String
Get
Return _Color
End Get
Set(ByVal Value As String)
_Color = Value
End Set
End Property
<XmlElement("font")> Public Property thisfont() As thisfont
Get
Return _Font
End Get
Set(ByVal Value As thisfont)
_Font = Value
End Set
End Property
Public Shared Sub save(ByVal path As String, ByVal config As Settings)
Dim xs As XmlSerializer = New XmlSerializer(GetType(Settings))
Dim fs As FileStream = New FileStream(path, FileMode.Create)
xs.Serialize(fs, config)
fs.Close()
End Sub

Public Shared Sub load(ByVal path As String, ByVal config As Settings)
Dim myObject As Settings
Dim xs As XmlSerializer = New XmlSerializer(GetType(Settings))
Dim fs As FileStream = New FileStream(path, FileMode.Create)
myObject = CType(xs.Deserialize(fs), Settings)
fs.Close()
End Sub
End Class


<XmlRoot("font")> Public Class thisFont
Private _name As String
Private _size As String
Private _bold As String
Private _italic As String
Private _strikeout As String
Private _underline As String
<XmlElement("name")> Public Property name() As String
Get
Return _name
End Get
Set(ByVal Value As String)
_name = Value
End Set
End Property
<XmlElement("size")> Public Property size() As Integer
Get
Return _size
End Get
Set(ByVal Value As Integer)
_size = Value
End Set
End Property
End Class
 
C

CJ Taylor

That's exactly what I needed! I just didn't know what it was called :'-(
Thanks for pointing me in the right direction.

Glad to know it...

As for how to do something kind of 'all in one'. Look into serailzing a
dataset, I'm pretty sure they are declared serializable. Even if not you
could make it that way, and use a binary formatter, serialize the dataset
out to the file, bring it back in.. And set the proper reference, your UI
would respond... and you wouldn't have to do any tricky binding code..

HTH
CJ
 

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