Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Can I do this?

Posted on 2008-06-16
10
Medium Priority
?
202 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
[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
  • 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 1

Accepted Solution

by:
trenduin earned 1000 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 1000 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

604 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