Solved

How do I instantiate a structure array within a structure?

Posted on 2008-06-17
11
275 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
 
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
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: 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

932 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now