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.