C# 如何制作可公开访问的课程列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15375265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make a list of classes publicly accessible?
提问by user2163343
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:
这样我就可以在 Windows 窗体应用程序中的任何位置访问和修改它,使用类似按钮单击的内容,如下所示:
MessageBox.Show(Vehicle[0].name)
I'm missing something. How do I make list Vehicle public?
我错过了一些东西。如何将车辆列表设为公开?
采纳答案by Mike C.
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)
}
回答by Dustin Kingen
If you want you list to be readonly
then you will need to only provide a getter
.
如果你希望你列出来,readonly
那么你只需要提供一个getter
.
public class Vehicles
{
private static readonly List<Vehicle> _vehicles = new List<Vehicle>();
public static List<Vehicle> Instance { get { return _vehicles; } }
}
回答by squelos
Actually, I would refer to this linkfor 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 ?
许多程序员认为使用静态是有害的,通常它是糟糕设计的标志。你真的需要有一个单例来访问你的类和对象吗?
回答by rhughes
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
在您的项目中创建一个文件,您可以在其中放置所有全局应用程序数据。例如:
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:
二是carList
在你的窗口类中创建变量。例如:
public class MyWindow : Window
{
#region Properties
public List<Vehicle> CarList
{
get
{
return this._carList;
}
}
private List<Vehicle> _carList = new List<Vehicle>();
#endregion
/* window stuff */
}
回答by internals-in
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.
静态类和类成员用于创建无需创建类的实例即可访问的数据和函数。静态类成员可用于分离独立于任何对象身份的数据和行为:无论对象发生什么,数据和函数都不会改变。当类中没有依赖于对象标识的数据或行为时,可以使用静态类。