Solved

How do I instantiate a structure array within a structure?

Posted on 2008-06-17
11
276 Views
Last Modified: 2013-12-16
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

0
Comment
Question by:rmmarsh
  • 6
  • 5
11 Comments
 
LVL 16

Assisted Solution

by:ororiole
ororiole earned 500 total points
ID: 21807012
Okay, How to instantiate the inner array.
First instantiate the outer struct, then the array.
            pricingData pd = new pricingData();
            pd.venueBookInfo = new bookInfoByVenue[5];  //price will be 0, newBook will be false, venue name will be null.
In this case you would have to know how many array items you have.

If you have a variable with a count
            pd.venueBookInfo = new bookInfoByVenue[count];
if you want to do compile time inits:
0
 

Author Comment

by:rmmarsh
ID: 21807110
Error: (2nd line above) Invalid token "=" in struct  
0
 
LVL 16

Expert Comment

by:ororiole
ID: 21807225
Which line? Copy and paste your line so I can see it.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 16

Expert Comment

by:ororiole
ID: 21807294
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)};
0
 

Author Comment

by:rmmarsh
ID: 21807317
2nd line...
            pricingData pd = new pricingData();
            pd.venueBookInfo = new bookInfoByVenue[100]; 

Open in new window

0
 
LVL 16

Expert Comment

by:ororiole
ID: 21808712
You must have a typo somewhere. Double check the lines before it and your struct declaration, cause I have it working in VS.
0
 

Author Comment

by:rmmarsh
ID: 21817504
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

0
 
LVL 16

Expert Comment

by:ororiole
ID: 21818367
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.
0
 

Author Comment

by:rmmarsh
ID: 21818380
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!
0
 
LVL 16

Accepted Solution

by:
ororiole earned 500 total points
ID: 21818633
You could put
venueBookInfo = new bookInfoByVenue[5];
inside the the constructor for the first structure.

But you should put them where ever you need to instantiate them, kinda like you would an object. If you are just going to use them temporarily, you should put them in the method that uses them. A structure is a value type, so it is instantiated on the stack, not the heap. When the method exits, the structures come off the stack. Thats the advantage to a struct. You wont run out of memory. (but if that array is really huge, you may have a stack overflow problem. The framework will adjust for that unless it is really enormous).

If you intended for them to be part of a class, then you could create them in the class constructor. In that case, they would be part of the class hence part of the heap so you wouldnt want to create that class thousands of times.
0
 

Author Comment

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

Thanks again...
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question