如何使课程列表可公开访问?
问题描述:
我希望可以从任何地方访问如下所示的对象列表,据我所知是单个类。
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);
这样我就可以在Windows Forms应用程序中的任何位置使用按钮单击来访问和修改它并显示以下内容:
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?
答
鉴于您所显示的,单例模式对您来说似乎有点合适现在。如果您不同意,请告诉我们,我们会为您指明正确的方向,现在,只需尝试以下操作即可:
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>();
}
您将像这样访问它:
private void SomeFunction()
{
MessageBox.Show(AcessStuff.CarList[0].Name)
}