rmmarsh
asked on
How do I instantiate a structure array within a structure?
Can't figure out how to instantiate "venueBookInfo"...
public struct pricingData // structure to hold the data obtained on a book
{
public string ISBN;
public decimal listPrice;
public bookInfoByVenue[] venueBookInfo;
}
public struct bookInfoByVenue
{
public string venueName;
public bool newBook;
public decimal price;
}
pricingData pD = new pricingData();
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Which line? Copy and paste your line so I can see it.
If you want to do compile time inits, you can use a constructor:
internal struct bookInfoByVenue
{
internal bookInfoByVenue(string name, bool newB, decimal pr)
{
venueName = name;
newBook = newB;
price = pr;
}
internal string venueName;
internal bool newBook;
internal decimal price;
};
pd.bookInfo = new bookInfoByVenue[] {new bookInfoByVenue( "2324-2222-1", false, 23.99M), new bookInfoByVenue("3445-1111 -4", true, 54.99M)};
internal struct bookInfoByVenue
{
internal bookInfoByVenue(string name, bool newB, decimal pr)
{
venueName = name;
newBook = newB;
price = pr;
}
internal string venueName;
internal bool newBook;
internal decimal price;
};
pd.bookInfo = new bookInfoByVenue[] {new bookInfoByVenue( "2324-2222-1", false, 23.99M), new bookInfoByVenue("3445-1111
ASKER
2nd line...
pricingData pd = new pricingData();
pd.venueBookInfo = new bookInfoByVenue[100];
You must have a typo somewhere. Double check the lines before it and your struct declaration, cause I have it working in VS.
ASKER
this is what I have... the errors are as follows:
Error 1 Invalid token '=' in class, struct, or interface member declaration
Error 2 Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
Error 3 Invalid token ';' in class, struct, or interface member declaration
Error 1 Invalid token '=' in class, struct, or interface member declaration
Error 2 Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
Error 3 Invalid token ';' in class, struct, or interface member declaration
public struct pricingData // structure to hold the data obtained on a book
{
public string ISBN;
public decimal listPrice;
public bookInfoByVenue[] venueBookInfo;
}
public struct bookInfoByVenue
{
public string venueName;
public bool newBook;
public decimal price;
}
pricingData pd = new pricingData();
pd.venueBookInfo = new bookInfoByVenue[5]; <--- all errors refer to this line
but are lines 14 and 15 inside a method somewhere? They can't just be hanging inside a namespace or something. You just have a compile error somewhere.
ASKER
Hmmm... you're right... they are after the declarations of the structure... can I put them inside the first structures constructor? or should they be in a method? (the problem with putting them in a method is that the method is called thousands of times, and I'm afraid I'll run out of memory!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you so much... I've learned a lot... wish I could give you more points, but I can't...
Thanks again...
Thanks again...
ASKER