A variable to use and change on multiple forms in 1 windows app

G

Guest

This is a C# 2005 project:

I'm struggling to figure how to make a variable accessible and changeable in
several forms in a windows application..

It's only a int variable which is set when the program loads, and this must
be accessible from any form within the application, and I also need to be
able to change it from any form.
Any hints or samples on how I can do this?

Thanks!
Svein Erik
 
M

Michael C

Svein Erik said:
This is a C# 2005 project:

I'm struggling to figure how to make a variable accessible and changeable
in
several forms in a windows application..

It's only a int variable which is set when the program loads, and this
must
be accessible from any form within the application, and I also need to be
able to change it from any form.
Any hints or samples on how I can do this?

Put this int in any class like this

class ABC
{
public static int MyInt;
}

then use it like this

ABC.MyInt = 5;

You can also create property set and get for it if you like.
 
G

Guest

Michael C said:
Put this int in any class like this

class ABC
{
public static int MyInt;
}

then use it like this

ABC.MyInt = 5;

You can also create property set and get for it if you like.
Thanks!
I went for the property set, and it works like a charm :)
But now i stumbled upon the next problem:

When i have 2 property sets; Track1 and Track2. How can i use the track that
the user selects on load?
If the user selects Track2, properties for Track2 is to be loaded:
.....properties.Track2.Default....
How can I set the Track2 text manually?

Thank you!
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

You will have to either wrap the class or use a third property

class ABC
{
public static int MyInt;

public static string GetValue{ get{ if (MyInt == 1 ) return "track 1"; else
return "track2"; } }
}
 
G

Guest

Hi, thanks for the reply!

But I mean, how can i use a string variable in the method that gets values
from a specified .settings file.

On load, the user selects which track he wants, if he selects track 2, then
the method will look like this: ...properties.Track2.Default.THEVALUE, and on
track 1 like this: properties.Track1.Default.THEVALUE.

It's the "string" Track1 and Track2 i want to replace with a variable that
is selected from the user.

Thank you :)
 

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