Cannot get structure value between forms

Simon

Freshman
Joined
Mar 3, 2003
Messages
25
Location
Vännäs, Sweden
Hello,
I hope i posted this to the right forum, otherwise, move me.

In my application the user shall be able to change settings, the settings are save to a file so that they hold their value until next time the user runs the application. Now, I've done like this:

In the settings form:
- when the form is loaded the settings file is opened and stored in the structure Settings. The settings are loaded into the controls on the form.
- user changes settings.
- user clicks 'save settings' and the settings are stored in the structure Settings.
- the values of the settings structure are saved to the settings file by random access.

When the application runs:
- the settings file is opened and the settings are stored in the structure Settings.

In this way the subroutines and functions should be able to get settings data from this Settings structure. My application consists of many classes and forms so each class has the following declaration;
Private Settings As FileIO.Settings
Now, I can refer the Settings structure in this way:
Settings.TheVariable
This, returns nothing though, TheVariable is empty. Confusing, because the Settings structure was set in the Load event of the first form. So I've tested it many times and discovered that the Settings structure has no values other than in the first form. Seems like Private Settings As FileIO.Settings makes a new, empty intstance of Settings.

The FileIO class and the Settings structure are declared in this way:
Public Class FileIO
Public Structure Settings
<VBFixedString(100)> Dim PicPath As String
<VBFixedString(100)> Dim SoundPath As String
<VBFixedString(1)> Dim BackColor As String
End Structure

The FileIO class is locate in the same file/form as the first form (but not within another class), could this be what causes it? I can't create a class that stands all alone which belongs to no form.
Seems like declaring the structure as Shared would be the right way, since shared variables have a lifetime that lasts thoughout the entire execution, until it's terminated. Visual Basic does not like this at all and says:

"Shared is not valid on a Structure declaration"

I hope you understand the problem :)

Regards,
Simon.
 
Okay, so i just solved the problem by declaring the variables as shared.

Public Structure Settings
<VBFixedString(100)> Shared PicPath As String
<VBFixedString(100)> Shared SoundPath As String
<VBFixedString(1)> Shared BackColor As String
End Structure
Visual Basic tells me that:
"Structure 'Settings' must contain at least one instant member variable or event declaration".
So I added a "Dim a as Integer" declaration just to get rid of the error:

Public Structure Settings
<VBFixedString(100)> Shared PicPath As String
<VBFixedString(100)> Shared SoundPath As String
<VBFixedString(1)> Shared BackColor As String
Dim a as integer
End Structure
So, this work just perfectly, but, why can't a structure contain only shared members?

Regards,
Simon.
 
Back
Top