Solved

Can I do this?

Posted on 2008-06-16
10
195 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
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…

839 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