如何使课程列表可公开访问? [英] How to make a list of classes publicly accessible?

查看:63
本文介绍了如何使课程列表可公开访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望可以从任何地方访问如下所示的对象列表,据我所知是单个类。

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)
}

这篇关于如何使课程列表可公开访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆