is it possible to store nested structs or classes in lists using c#?
looking at the following code segments.
Nested Scruct:
struct structBooks
{
public string strBookName;
public string strAuthor;
public structPubished publishedDate;
}
struct structPubished
{
public int intDayOfMonth;
public int intMonthOfYear;
public int intYear;
}
saving as a list:
static void AddBookToList()
{
structBooks testStruct = new structBooks();
testStruct.strBookName = newBookName;
testStruct.strAuthor = newAuther;
testStruct.publishedDate.intYear = intNewYear;
testStruct.publishedDate.intMonthOfYear = intNewMonthOfYear;
testStruct.publishedDate.intDayOfMonth = intNewDayOfMonth;
static List<structBooks> listBooks = new List<structBooks>();
listBooks.Add(new structBooks()
{
strBookName = newBookName,
strAuthor = newAuther,
publishedDate.intYear = intNewYear,
publishedDate.intMonthOfYear = intNewMonthOfYear,
publishedDate.intDayOfMonth = intNewDayOfMonth
});
}
creating all the testStruct's works as expected.
When it comes to storing the struct as a list strBookName and strAuthor both work. However, when it comes to the nested publishedDate Visual Studio tells me "invalid initialiser member declarator".
the list its self is defined in the Main method, I just added it so you can see how it's defined.
what am i missing?