473,796 Members | 2,765 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1763
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******** *******@TK2MSFT NGP11.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.ForeColo r = System.Drawing. ColorTranslator .FromOle(mySett ings.color)

Label1.Font.Nam e = mySettings.this font.name

Label1.Font.Siz e = mySettings.this font.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.colo r = System.Drawing. ColorTranslator .ToOle(Label1.F oreColor)

mySettings.this font.name = Label1.Font.Nam e

mySettings.this font.size = Label1.Font.Siz e

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

'class1.vb contains:

Imports System.Xml
Imports System.Xml.Sche ma
Imports System.Xml.Seri alization
Imports System.IO

<XmlRoot("confi g")> 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("fo nt")> 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(G etType(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(G etType(Settings ))
Dim fs As FileStream = New FileStream(path , FileMode.Create )
myObject = CType(xs.Deseri alize(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("na me")> Public Property name() As String
Get
Return _name
End Get
Set(ByVal Value As String)
_name = Value
End Set
End Property
<XmlElement("si ze")> 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.ForeColo r =
System.Drawing. ColorTranslator .FromOle(mySett ings.color)

Label1.Font.Nam e = mySettings.this font.name

Label1.Font.Siz e = mySettings.this font.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.colo r = System.Drawing. ColorTranslator .ToOle(Label1.F oreColor)

mySettings.this font.name = Label1.Font.Nam e

mySettings.this font.size = Label1.Font.Siz e

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

'class1.vb contains:

Imports System.Xml
Imports System.Xml.Sche ma
Imports System.Xml.Seri alization
Imports System.IO

<XmlRoot("confi g")> 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("fo nt")> 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(G etType(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(G etType(Settings ))
Dim fs As FileStream = New FileStream(path , FileMode.Create )
myObject = CType(xs.Deseri alize(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("na me")> Public Property name() As String
Get
Return _name
End Get
Set(ByVal Value As String)
_name = Value
End Set
End Property
<XmlElement("si ze")> 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
1191
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() at the prompt, I get >>>
9
3650
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 for my session variable based security scheme. Basically, the risk is that a user will login to my site, close the window when done and allow someone else to come up to the machine, go back to my site and be logged into the previous user's...
1
2010
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: Developer Address: 123 Main Street. After the table of information will be the web form. So basically I want to display the table and then have the web form.
2
2364
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: Developer Address: 123 Main Street. After the table of information will be the web form. So basically I want to display the table and then have the web form.
1
1784
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 modified the web.config file to enableSessionState, but still the variables are getting lost. I found a knowledgbase article 316112 - PRB Session Variables Do Not Persist Between Requests After You Install Internet Explorer Security Patch MS01-055.htm...
1
1541
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") FormsAuthentication.SetAuthCookie(lngID, False) and then redirect to another page. On that page, when I try: Response.Write(Session("strname")) Response.Write(Page.User.Identity.Name)
5
2369
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 FormMode { get { object formMode = this.ViewState;
1
1893
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 strange, but I feel I'm missing an obvious answer. Perhaps you guru's can enlighten me? I have webform. I also have a class, which includes the following declaration and property:
2
1643
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 curious about how others persist information such as login IDs etc while each user is logged in. Previously I've used a hidden form with text fields on it to keep this information persisted, but I'm wondering if anyone would suggest other methods?
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10465
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10200
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9061
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5453
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4127
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 we have to send another system
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.