What's the normal way to handle global parameters?

S

Steve Howard

I have some global parameters that I want to apply throughout my
application. One is SoundOn, for instance.

I want to be sure that I am doing things the 'right way', so rather than
make assumptions, or take the first solution I find in the first web
reference I see, I'd like to ask here what the normal way would be to handle
such parameters.

I figured there would be two main possibilities :-


1, Write the parameter to the registry, and read it whenever it needs to be
used. Re-write it to the registry if it changes for any reason.
2, Write the parameter to the registry, read it once on startup and have
every interested form or object point to a global reference. Re-write it to
the registry if it changes for any reason, and update global reference at
the same time.


The first is obviously wasteful (potentially hundreds of registry reads per
session), but would certainly work. The second seems logical, but I don't
want to assume it's the best method.

Or is there another way?

If a global reference is used, is there a convention for doing so? For
instance would I build a special class that is used only for parameters?


TIA


Steve
 
M

Mark Arteaga

Hello Steve,

I would create a class that encapsulates all your settings in properties.
For example:

public class Settings
{
public static SoundOn
{
get
{
//Get the value
}
set
{
//set the value
}
}
}

I would also use an XML file instead of the registry. You can get the value
either every time or store the value in an internal variable. Just make
sure that if you change the value (the setter) you update your internal variable.

HTH
________________________________________
Mark Arteaga
..NET Compact Framework MVP
http://www.neotericsdc.com | http://blog.markarteaga.com
 
S

Steve Howard

I would create a class that encapsulates all your settings in properties.
--

I would also use an XML file instead of the registry. You can get the
value either every time or store the value in an internal variable. Just
make sure that if you change the value (the setter) you update your
internal variable.


Perfect, thank you!

Steve
 
S

Steve Howard

I would create a class that encapsulates all your settings in properties.

OK I thought I understood this.

I created a new cs file called Globals. I added this code:-


using System;

using System.Collections.Generic;

using System.Text;

using System.Windows.Forms;

namespace LearntoFly

{

public class Globals

{

private bool soundOn;

public bool SoundOn

{

get { return soundOn; }

set {

soundOn = !soundOn;

MessageBox.Show(soundOn.ToString());

}

}

}

}



Then I went to my main form, and in a ToolBar button I tried to set SoundOn



SoundOn();



I get the error "The name 'SoundOn' does not exist in the current context. I
tried adding Globals with using Globals, using LearntoFly.Globals - no
joy. I messed about with public/private for class Globals and have tried a
score of other things.



So now I know I missunderstand how I should be able to access this Global
class and it's properties. Could someone explain to me what I missed?





TIA



Steve
 
C

Chris Tacke, eMVP

They need to be static methods and variable (I'd make the whole class static
in fact) so you don't need a class instance to access them.

--
Chris Tacke
Co-founder
OpenNETCF.org
Are you using the SDF? Let's do a case study.
Email us at d c s @ o p e n n e t c f . c o m
http://www.opennetcf.org/donate
 
S

Steve Howard

They need to be static methods and variable (I'd make the whole class
static in fact) so you don't need a class instance to access them.


Thanks Chris. I changed them all to static, but I still get the same error
:-(

namespace LearntoFly

{

static class Globals

{

private static bool soundOn;

static bool SoundOn

{

get { return soundOn; }

set

{

soundOn = !soundOn;

MessageBox.Show(soundOn.ToString());

}

}

}

}
 
S

Steve Howard

I got it to work by doing this

public static class Globals

{

private static bool soundOn = true;

public static bool SoundOn

{

get { return soundOn; }

set {

soundOn = !soundOn;

MessageBox.Show(soundOn.ToString());

}

}

}





Steve
 
M

Mark Arteaga

Hello Steve,

If it's another project make sure you reference the project. Also make your
class public and your properties public also.

You should then be able to access it using LearntoFly.Globals.SoundOn from
your form
________________________________________
Mark Arteaga
..NET Compact Framework MVP
http://www.neotericsdc.com | http://blog.markarteaga.com
 

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