Link to home
Start Free TrialLog in
Avatar of CraigBroadman
CraigBroadman

asked on

Array Of Structs

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






ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of CraigBroadman
CraigBroadman

ASKER

I have actually used an Array List now

Thanks all