Hello folks
:)
As you see,
I'm new at this forum as well as in C#.
I build a simple program using M$ Visual Studio, and I've got some problems concerning
classes. I have been always working with C++, where global variables used to
exist.
I have
created three files (.cs) which contain the following classes: Item and List. The
program is just going to sort a list containing certain items. Each class has
its properties, but the List class has one or many items (Item class).
My application
has two forms. My main problem is that I cannot access an instance of the List
class from all my forms or any other part of the program. A friend of mine told
me that I should review my object-oriented concepts, but I tried it a lot of times
:(
How can I
solve that? Can somebody help me?
Sorry for my bad-english :P
Scott LyslePosted Apr 30, 2008, 4:48 AM
Well, if you really want to have global variables, you can still do that, just set up a static class and create a collection of public static variables within that class. You can access them (read/write) from anywhere in the application.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GlobalVariables
{
public static class GlobalVars
{
public static string sUserName;
public static string sPassword;
}
}
Or, you could create user scoped settings in you application's properties and use those like you would have used global variables.
private void btnPropertySetting_Click(object sender, EventArgs e)
{
Properties.Settings.Default.MyGlobalVariable = "Wonderpig flies again";
Properties.Settings.Default.Save();
MessageBox.Show(Properties.Settings.Default.MyGlobalVariable);
}