Solved

Can I do this?

Posted on 2008-06-16
10
191 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

18 Experts available now in Live!

Get 1:1 Help Now