[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Array Of Structs

Posted on 2006-06-05
3
Medium Priority
?
273 Views
Last Modified: 2010-04-16
Hi

I have the below Struct

struct staff
    {
        public string fullName;
        public string monthYear;
        public int hits;

        public staff(string fullName, string monthYear, int Hits)
        {
            this.fullName = fullName;
            this.monthYear = monthYear;
            this.hits = Hits;
        }
    }

And then I have a Stored Procedure that gets a list of hits for various employees, see example below

Jim, June, 50
Jim, May, 34
Jim, April, 26
Simon, June, 22
Simon, May, 20
Simon, April, 21
Sam, June, 44
Sam, May, 15
Sam, April, 19

What I then want to do is put this information into an array of structs, all I have got so far is.........

Obviously looping through the records and.......

staff staffDetail = new staff(reader["Employee"].ToString(), reader["HitMonth"].ToString(), Convert.ToInt32(reader["Hits"].ToString()));

                                    staff[] staffList = { staffDetail };

But this is obviously wrong, i'm new to C# and new to Structs, any information very gratefully received.

Craig






0
Comment
Question by:CraigBroadman
3 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 150 total points
ID: 16831457
You'd be better off with something a bit more flexible like an ArrayList (or if using .Net 2.0 a List):

    ArrayList staffList = new ArrayList();          // Outside of loop


    // Inside loop
    staff staffDetail = new staff(reader["Employee"].ToString(), reader["HitMonth"].ToString(), Convert.ToInt32(reader["Hits"].ToString()));
    staffList.Add(staffDetail);

You can then retrieve the "staff" member you want using array subscripting:

    staff person = (staff)staffList[0];
0
 
LVL 11

Expert Comment

by:Agarici
ID: 16831462
you have to do
staff[] staffList = new staffList[2];

then
staffList[0] = staffDetail ;
staffList[1] = staffDetail ;

you can also do
ArrayList al = new ArrayList();
al.Add( staffDetail );

but, pay attention since structs are passed by value in c#

hth,
A.
0
 

Author Comment

by:CraigBroadman
ID: 16832131
I have actually used an Array List now

Thanks all
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month20 days, 3 hours left to enroll

873 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