1

I am creating an application using Xamarin.Forms. I am trying to access each element in the list of MovieRent instances. I have a class that stores movie information:

 public class MovieRent
    {
        public string movieTitle { get; set; }
        public string movieReleaseDate { get; set; }
        public int movieDuration { get; set; }
        public double movieRentalPrice { get; set; }
        public string movieRentType { get; set; }

        public MovieRent(string mtitle, string mdate, int mtime, double mprice, string type)
        {
            movieTitle = mtitle;
            movieReleaseDate = mdate;
            movieDuration = mtime;
            movieRentalPrice = mprice;
            movieRentType = type;
        }
    }

addCartList is called to add a movie to a list. This creates/stores a list of movies.

 class MoviesToRent
    {
        public List<MovieRent> movieRentList;
        public MoviesToRent()
        {
            movieRentList = new List<MovieRent>();
        }

        public static MoviesToRent addCartList(string title, string date, int duration, double price, string type)
        {
            MoviesToRent movieList = new MoviesToRent();

            MovieRentnewMovie = new MovieRent(title, date, duration, price, type);

            movieList.movieRentList.Add(newMovie);

            return movieList;
        }
    }

I am trying to access each instance, for example, the title of each movie in the list:

foreach (string movie in MoviesToRent.movieRentList) 
{
   this.Movie_Info.Text = movieRentList.title;
}

However, I get this error:

An object reference is required for the nonstatic field, method, or property 'MoviesToRent.movieRentList'.

How can I overcome this error, so that I can access the movies that are in the list? I am trying display all movie info by looping through the list. Thank you.

3 Answers3

0

Your trying to access movieRentList as a static property. You need a instance of MoviesToRent and then access the list.

var movieRents = new MoviesToRent();
for (var movieRent in movieRents.movieRentList) 
{

}

PS: In the example above, the list will be empty because I didnt add any object to it.

0

Okay, I think I first have to fix that naming and some glaring code issues. It is confusing the heck out of me:

 public class Movie
    {
        public string movieTitle { get; set; }
        public string movieReleaseDate { get; set; }
        public int movieDuration { get; set; }
        public double movieRentalPrice { get; set; }
        public string movieRentType { get; set; }

        public MovieRent(string mtitle, string mdate, int mtime, double mprice, string type)
        {
            movieTitle = mtitle;
            movieReleaseDate = mdate;
            movieDuration = mtime;
            movieRentalPrice = mprice;
            movieRentType = type;
        }
    }


class MoviesRentCart
    {
        public List<Movie> movieRentList;
        public MoviesToRent()
        {
            movieRentList = new List<MovieRent>();
        }

        public static MoviesToRent addCartList(string title, string date, int duration, double price, string type)
        {
            //What does this line do here?
            //MoviesToRent movieList = new MoviesToRent();

            //fixed missing space
            Movie newMovie = new Movie(title, date, duration, price, type);

            movieList.movieRentList.Add(newMovie);

            return movieList;
        }
    }

Then in your actuall code, you need a instance of MovieRentCart. Same way you make an instance of Movie. To that instance, you add the elements. Over that instance's movieRentList, you itterate.

Christopher
  • 9,634
  • 2
  • 17
  • 31
0

The error happens because you're trying to access movieRentList as if it where a static field member of the class MoviesToRent. You need an instance of MoviesToRent like so:

var moviesToRent = new MoviesToRent();

And to access the title of each movie in the list do:

foreach (MovieRent movie in moviesToRent.movieRentList)
{
   //The movie title is movie.movieTitle
}
Javier Silva Ortíz
  • 2,864
  • 1
  • 12
  • 21