Solved

How do I instantiate a structure array within a structure?

Posted on 2008-06-17
11
278 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

730 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