问题
I want a list of objects like the following to be accessible from anywhere, a "singleton" class as I understand.
public class Car
{
public string Name;
public string Color;
public int Value;
}
List<Vehicle> carList = new List<Vehicle>();
Car jeep = new Car();
jeep.Name = "Jeep";
jeep.Color = "Red";
jeep.Value = 20000;
Vehicle.Add(jeep);
Such that I can access and modify it anywhere in a Windows Forms application, using something like a button click with the following:
MessageBox.Show(Vehicle[0].name)
I'm missing something. How do I make list Vehicle public?
回答1:
Given what you've shown, a singleton pattern seems a bit much for you right now. If you disagree, just let us know and we'll point you in the right direction, for now, just try something like this:
public class AcessStuff
{
public static List<Vehicle> CarList = new List<Vehicle>();
}
And you would access it like this:
private void SomeFunction()
{
MessageBox.Show(AcessStuff.CarList[0].Name)
}
回答2:
If you want you list to be readonly
then you will need to only provide a getter
.
public class Vehicles
{
private static readonly List<Vehicle> _vehicles = new List<Vehicle>();
public static List<Vehicle> Instance { get { return _vehicles; } }
}
回答3:
Actually, I would refer to this link for the singleton
Here is one version :
public sealed class Singleton
{
private static readonly Singleton instance = new Singleton();
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static Singleton()
{
}
private Singleton()
{
}
public static Singleton Instance
{
get
{
return instance;
}
}
}
Many programmers consider using the static as harmful, usually it is a sign of a bad design. Do you really need to have a singleton to access your classes and objects ?
回答4:
There are two ways that you can accomplish this.
One is to create a Data.cs
file in your project whereby you put all of your global application data. For instance:
Data.cs
public static class ApplicationData
{
#region Properties
public static List<Vehicle> CarList
{
get
{
return ApplicationData._carList;
}
}
private static List<Vehicle> _carList = new List<Vehicle>();
#endregion
}
Two is to create the variable carList
in your window class. For instance:
public class MyWindow : Window
{
#region Properties
public List<Vehicle> CarList
{
get
{
return this._carList;
}
}
private List<Vehicle> _carList = new List<Vehicle>();
#endregion
/* window stuff */
}
回答5:
use static objects thats all
public static List<string> vehicles =new List<string> ();
Static classes and class members are used to create data and functions that can be accessed without creating an instance of the class. Static class members can be used to separate data and behavior that is independent of any object identity: the data and functions do not change regardless of what happens to the object. Static classes can be used when there is no data or behavior in the class that depends on object identity.
来源:https://stackoverflow.com/questions/15375265/how-to-make-a-list-of-classes-publicly-accessible