Array Of Structs

Posted on 2006-06-05
Last Modified: 2010-04-16

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.


Question by:CraigBroadman
    LVL 52

    Accepted Solution

    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()));

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

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

    Expert Comment

    you have to do
    staff[] staffList = new staffList[2];

    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#


    Author Comment

    I have actually used an Array List now

    Thanks all

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    746 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

    16 Experts available now in Live!

    Get 1:1 Help Now