Solved

Can I do this?

Posted on 2008-06-16
10
193 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

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…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

896 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

21 Experts available now in Live!

Get 1:1 Help Now