[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do I instantiate a structure array within a structure?

Posted on 2008-06-17
11
Medium Priority
?
288 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 2000 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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 2000 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month18 days, 4 hours left to enroll

830 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