Link to home
Start Free TrialLog in
Avatar of rmmarsh
rmmarshFlag for United States of America

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();

Open in new window

SOLUTION
Avatar of ororiole
ororiole
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rmmarsh

ASKER

Error: (2nd line above) Invalid token "=" in struct  
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)};
Avatar of rmmarsh

ASKER

2nd line...
            pricingData pd = new pricingData();
            pd.venueBookInfo = new bookInfoByVenue[100]; 

Open in new window

You must have a typo somewhere. Double check the lines before it and your struct declaration, cause I have it working in VS.
Avatar of rmmarsh

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

        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       

Open in new window

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.
Avatar of rmmarsh

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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rmmarsh

ASKER

Thank you so much... I've learned a lot... wish I could give you more points, but I can't...

Thanks again...