I have been working on a project for my c# class at school. And I have a very simple question I think. But I have been unable to find an answer anywhere. I keep getting results about how to make a list of structs. I want to know how to access a list inside a struct?
So here is the struct given to us by our teacher and that we must use for this assignment:
[Serializable]
struct Name
{
public string firstName;
public string lastName;
}
[Serializable]
struct Movie
{
public string title;
public string year;
public Name director;
public float quality;
public string mpaaRating;
public string genre;
public List<Name> cast;
public List<string> quotes;
public List<string> keywords;
}
struct MovieList
{
public int length;
public Movie[] movie;
}
Now I have tried accessing quotes and keywords in the following two ways and both have produced errors:
1.
string quotes;
MovieList ML = new MovieList();
quotes = Console.ReadLine();
ML.movie[0].quotes[0] = quotes;
2.
string quotes;
MovieList ML = new MovieList();
quotes = Console.ReadLine();
ML.movie[0].quotes.Add(quotes);
A struct is a
Valuetype and as such, using a struct to carry all of this information makes it very inefficient because every time you pass it as an argument the whole contents of the struct will need to be copied, etc. a better approach would be to use a Class, which is aReferencetype and it’s reference is what gets passed around.As far as how to access your struct members, here’s an example:
UPDATE:
Showing how to access quotes: