Solved

Can I do this?

Posted on 2008-06-16
10
194 Views
Last Modified: 2013-12-17
I want to make a structure within a structure (see below)... can I do this? (getting compile errors, so I think my syntax is incorrect).
internal struct pricingData  //  structure to hold the data received on each book received from website
        {
            internal string ISBN;
            internal decimal listPrice;
            internal struct bookInfoByVenue[]
            {
                internal string venueName;
                internal bool newBook;
                internal decimal price;
            };
        };
        internal pricingData pD = new pricingData();

Open in new window

0
Comment
Question by:rmmarsh
  • 4
  • 4
  • 2
10 Comments
 
LVL 16

Expert Comment

by:ororiole
ID: 21798417
Yes you can make a struct within a struct. But what you've done so far (if we throw the array [] out) is just make a structure definition within a struct.

I think what you want is something like


    internal struct bookInfoByVenue
    {
        internal string venueName;
        internal bool newBook;
        internal decimal price;
    };
 
    internal struct pricingData  //  structure to hold the data received on each book received from website
        {
            internal string ISBN;
            internal decimal listPrice;
            internal bookInfoByVenue[5] bookInfo;
 
        };

Open in new window

0
 
LVL 1

Expert Comment

by:trenduin
ID: 21798423
You're trying to create your second structure as an array. If you need an array of structures, then create the second structure separately. I have included a correction of your original code and an alternative. I tested both and they seem to work.

Also, I'm guessing you didn't post the full code. Using Internal will require that you include get/set methods for your variables. If you have a lot of methods in a structure, MSDN recommends you use a class instead.

Hope this helps. =o)
//this is a corrected version of your originally posted code
internal struct pricingData  //  structure to hold the data received on each book received from website
        {
            internal string ISBN;
            internal decimal listPrice;
            internal struct bookInfoByVenue
            {
                internal string venueName;
                internal bool newBook;
                internal decimal price;
            }
        }
 
//use this option if you want to have an array of structures
        internal struct pricingData  //  structure to hold the data received on each book received from website
        {
            internal string ISBN;
            internal decimal listPrice;
            internal bookInfoByVenue[] myBookVen;
        }
        internal struct bookInfoByVenue
        {
            internal string venueName;
            internal bool newBook;
            internal decimal price;
        }

Open in new window

0
 

Author Comment

by:rmmarsh
ID: 21805498
trendiun:  what's "a lot"?  I would need one get/set for each variable, right? (I am going to use the array of structures)
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 1

Accepted Solution

by:
trenduin earned 250 total points
ID: 21805741
If you want an array of structures then I recommend that you use the second option. You will need get/set for your variables, plus you'll probably need to add a method to add items to your array of bookInfoByVenue. If there is a specific reason why you need it to be a structure, then it should be fine, but if you could, I'd use a class instead.
0
 
LVL 16

Assisted Solution

by:ororiole
ororiole earned 250 total points
ID: 21805743
Its always a good idea to access your variables thru get/set (properties are recommended for this) but since they are internal, you do not have to. Internal means that the object is accessable like public, except only within the assembly. If you are using it within your assembly, you have full access to the fields.

If you are going to make it a class library, and then use it from another assembly,  then you would have to expose them thru public properties.
0
 

Author Comment

by:rmmarsh
ID: 21805848
trenduin:  how do I instantiate the inner array (myBookVen)?  I tried to run it, but it's null...

ororiole:  I'm going to leave it internal so I don't have to do the get/set thingie... pain in the tokus and I would rather just do an assignment...
0
 

Author Comment

by:rmmarsh
ID: 21805976
trenduin:  how do I instantiate the inner array (myBookVen)?  I tried to run it, but it's null...

ororiole:  I'm going to leave it internal so I don't have to do the get/set thingie... pain in the tokus and I would rather just do an assignment...
0
 

Author Closing Comment

by:rmmarsh
ID: 31467789
Thanks... now get another 500 points by figuring out how to instantiate the inner structure array!
0
 
LVL 1

Expert Comment

by:trenduin
ID: 21824591
To instantiate the array you will need to add some get, set methods along with some other methods to add and remove items from the array.
0
 
LVL 1

Expert Comment

by:trenduin
ID: 21888454
Ok here's what I did, seems to work fine. The structure information was put in a separate class.
//code for the structure setup
    internal struct pricingData  //  structure to hold the data received on each book received from website
    {
        internal string ISBN;
        internal decimal listPrice;
        internal struct bookInfoByVenue
        {
            internal string venueName;
            internal bool newBook;
            internal decimal price;
        }
        internal bookInfoByVenue[] myVenue;
    }
 
 
//this was in a form
        private void Form1_Load(object sender, EventArgs e)
        {
            pricingData mydata = new pricingData();
            mydata.myVenue = new pricingData.bookInfoByVenue[10];
        }

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

803 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