Solved

How do I instantiate a structure array within a structure?

Posted on 2008-06-17
11
274 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
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)…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

746 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

11 Experts available now in Live!

Get 1:1 Help Now