473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Persist Variables

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/de...ssolutions.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
Nov 21 '05 #1
3 1743
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..
"Matthew" <tu*************@alltel.net> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
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/de...ssolutions.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

Nov 21 '05 #2
> 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.ForeCo lor)

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
Nov 21 '05 #3
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
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.ForeCo lor)

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

Nov 21 '05 #4

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

Similar topics

2
by: Thomas Philips | last post by:
Here's a very simple program with an odd twist: class Player(object): def __init__(self,name): self.name = name hero = Player("A") print "hero",hero If I run it in IDLE and then type dir()...
9
by: Pack Fan | last post by:
I've noticed that session variables will persist on Mac IE even after all browser windows have been closed. One must quit the program to clear the session variables. This presents a security risk...
1
by: Richard Dixson | last post by:
Under ASP.NET (C#) I want to create a page that people can use to submit questions. This page will consist of a table with several rows of information, like: Name: Bill Smith Job title:...
2
by: Richard Dixson | last post by:
Under ASP.NET (C#) I want to create a page that people can use to submit questions. This page will consist of a table with several rows of information, like: Name: Bill Smith Job title:...
1
by: Dew Baboeram | last post by:
Hi Some mystery. Until I updated my windows xp I could preserve session variables through aspx pages. Now the session variables are getting lost. I cannot preserve them. I have explicitly...
1
by: Raed Sawalha | last post by:
Hello: My problem is that in my login page, I authenticate the user and set a session variable: Session("strname") = dr("userlname") & ", " & dr("userfname") & " " & dr("usermi")...
5
by: Usenet User | last post by:
I know I can use ViewState for member fields and variables declared on Page level to persist between postbacks, for example: private const string FormModeTag = "_FormMode_"; private string...
1
by: Owen | last post by:
Firstly, if this is the wrong newsgroup for my question, please direct me elswhere and accept my apologies! It's a question about ASP.NET but also uses VB.NET classes. This is a little...
2
by: planetthoughtful | last post by:
Hi All, I'm returning to coding in Access after a break of about 4 years, and designing a multi-user app split between a front end mdb for forms / code etc and a backend mdb for tables. I'm...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
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,...
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,...
0
jinu1996
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 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.