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

时间:2019-04-11 09:33:29

标签: c# object generics scope generic-collections

我有一个课堂电影,在这里我有自动实现的列表“ Shows”来添加Show类对象。我可以通过main方法添加和访问它,但是当我从另一个名为Access的类中调用列表时,它给了我一个空列表。

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

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

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

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

4 个答案:

答案 0 :(得分:1)

欢迎使用Stackoverflow。请仔细阅读您的代码。在Main方法中,创建新的Movie对象(我们称其为movieA),然后向Show添加新的movieA对象。在Access类中,您没有使用MovieA,但是再次创建了一个新的Movie对象。如果要访问在Movie类中的Main方法中创建的Access,则必须在其中传递它。

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

答案 1 :(得分:0)

您应该为Access类提供一个参数化的构造函数,该构造函数接受Movie对象。

答案 2 :(得分:0)

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

答案 3 :(得分:0)

问题似乎是合乎逻辑的,因为每次实例化电影新的显示列表(显示)时,都要在构造函数中实例化显示列表(显示)。 strong>)将被实例化。

  

您必须使用单例模式

单例模式是一种创新设计模式,可保证对象的单个实例。

private static List<Show> shows;

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