当从另一个 class 引用时,列表变为空,我在哪里做错了?

The lists become empty when referenced from another class, where am i doing it wrong?

我有一部 class 电影,我在其中自动执行了 List 'Shows' 以添加 Show class 对象。我可以在 main 方法中添加和访问它,但是当我从另一个名为 Access 的 class 调用列表时,它会给我一个空列表。

我在 Access class 中实例化了 Movie class 但它正在创建新列表而不是获取已经存在的列表。

我需要在 Movie class 中有一个非参数化的构造函数。我还需要能够在其他 class 中访问相同的 List 'Shows'。

// The Movie class where i create the list and store all show class objects

public class Movie
{
        public List<Show> Shows { get; set; }

        public Movie()
        {
            this.Shows = new List<Show>();
        }

        public static void Main(string[] args)
        {

            // create new object of Show type
            Show s = new Show(153, 258, 391);

            Movie mv = new Movie();

            // add object to List
            mv.Shows.Add(s);

            // The output gives me 153, which is correct
            Console.WriteLine(mv.Shows.ElementAt(0).ShowID);
        }
    }

    public class Show
    {
        public int ShowID { get; set; }
        public int MovieID { get; set; }
        public int TheatreID { get; set; }

        public Show(int showid, int movieid, int theatreid)
        {
                this.ShowID = showid;
                this.MovieID = movieid;
                this.TheatreID = theatreid;
        }
    }

   // i need to Access the list in this class
   public class Access
   {
       Movie mov = new Moive();

       // the output is showing null value error
       Console.WriteLine(mov.Shows.ElementAt(0).ShowID);
   }

我需要能够从同一名称空间中的其他 classes 获取和设置列表

您应该有一个用于 Access class 的参数化构造函数,它接受 Movie 对象。

欢迎使用 Whosebug。请仔细阅读您的代码。在 Main 方法中,您创建新的 Movie 对象(我们称之为 movieA),并将新的 Show 对象添加到 movieA。在 Access class 中,您没有使用 MovieA,但您再次创建了一个新的 Movie 对象。如果你想访问在 Main 方法中创建的 Movie,在 Access class 中,你必须将它传递到那里。

public static void Main(string[] args)
{

    // create new object of Show type
    Show s = new Show(153, 258, 391);

    Movie movieA = new Movie();

    // add object to List
    movieA.Shows.Add(s);

    // The output gives me 153, which is correct
    Console.WriteLine(movieA.Shows.ElementAt(0).ShowID);

    var access = new Access();
    access.MyMethod(movieA);
}

public class Access
{
   public void MyMethod(Movie movie)
   {
       // this should work
       Console.WriteLine(movie.Shows.ElementAt(0).ShowID);
   }
}

Access 中创建一个新的 Movie,而您需要使用已经创建的 Movie

public class Access
{
    public void ShowMovieOutput(Movie mov)
    {
        Console.WriteLine(mov.Shows.ElementAt(0).ShowID);
    }
}

public static void Main(string[] args)
{
    // create new object of Show type
    Show s = new Show(153, 258, 391);

    Movie mv = new Movie();

    // add object to List
    mv.Shows.Add(s);

    Access.ShowMovieOutput(mv);

    // The output gives me 153, which is correct
    Console.WriteLine(mv.Shows.ElementAt(0).ShowID);
}

这个问题似乎合乎逻辑,因为你在构造函数中实例化了你的节目列表 (Shows),每次你实例化 Movie 一个新节目列表 (Shows) 将被实例化。

In your case you must use singleton pattern

单例模式是一种创建型设计模式,它保证了一个对象的单一实例。

private static List<Show> shows;

public static List<Show> Shows
{
    get
    {
        if (shows == null)
        {
            shows = new List<Show>();
        }
        return shows;
    }
 }