简体   繁体   中英

Modify a List Inside of a Struct

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 Value type 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 a Reference type and it's reference is what gets passed around.

As far as how to access your struct members, here's an example:

MovieList m =new MovieList();
m.movie = new Movie[10];
m.movie[0].title="The Girl with the Dragon Tatoo";
Console.WriteLine(m.movie[0].title); //The Girl with the Dragon Tatoo

UPDATE:

Showing how to access quotes:

MovieList m =new MovieList();
m.movie = new Movie[10];
m.movie[0].title="The Girl with the Dragon Tatoo";
m.movie[0].quotes = new List<string>();
m.movie[0].quotes.Add("Hello World");
Console.WriteLine(m.movie[0].title); //The Girl with the Dragon Tatoo

Console.WriteLine(m.movie[0].quotes[0]); //Hello World

Your MovieList struct contains an array of Movie . The array isn't being initialized.

Why not just make a List<Movie> instead of a separate struct or class?

thanks for being honest about your homework. The first problem I see is that you are trying to use the list before they are initialized. Inside the structures, brand-new list are referencing to null. You have to initialize them. Please do a research about struct initialization.

Make these classes rather than structs. For every Movie in your MovieList class you will have to create a new instance of Movie so you can add to the quotes list. Otherwise it wont be initialised.

Structures should generally avoid exposing fields or properties of mutable class types, except in cases where the struct will be used by code which is merely interested in the identity of the objects referred to therein, rather than their content . Suppose one has structures m1 , which contains information about some movie, and m2 , which is initially blank. If one executes code:

m2 = m1;
  m2.year = 2012;
  m2.keywords.Add("dinosaur")

Then m1.year will be unmodified, but m1.keywords will have "dinosaur" added to it (since m1.keywords and m2.keywords both refer to the same mutable List<string> .

It's fine for structs which are used as data-holders to expose read-write fields of logically-immutable class types, or value types which don't contain any mutable class types, but structs which hold mutable reference types often have weird semantics and should be avoided when practical.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM