Link to home
Start Free TrialLog in
Avatar of thaburner
thaburnerFlag for United States of America

asked on

Read/Write Array in Array

I'm new to C# and trying to figure out the best/easiest way to handle this. What I need to accomplish is having (ID, Host, EmployeeID, Permission, Value) as an array inside another array (Employee).  The code I included is from PHP of how I handled this. If I can get an example of how to read/write this or get pointed in the right direction that would be very helpful.

   
foreach ($_SESSION['Employee'] as &$value) {;
	$ReturnArray[] = array("ID" => $value["ID"],
			       "Host" => $value["Host"],
			       "EmployeeID" => $value["EmployeeID"],
			       "Permission" => $value["Permission"],
			       "Value" => $value["Value"]);
		
}

Open in new window

Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

The best way to do it is not as an array inside of an array, but with a small class.

Here is, in its simplest form, the way to create your class, usually in its own file. You will have to adjust the types to the type of values you use. Properties would be a better programming practice, but would take more work to code, unless you were in VB or unless they have a shortcut I do not know about in C#.

public class Employee
{
   public int ID;
   public string Host;
   public int EmployeeID;
   public string Permission;
   public int Value;
}

Open in new window

In the application, you declare and use your array this way:
Employee[] Employees = new Employee[10];

Employees[0].ID = 1;
Employees[0].EmployeeID = 2;
Employees[0].Host = "Some host";
//...
Employees[10].ID=34;

//...
Console.WriteLine ( Employees[3].Host );

Open in new window

Your second array becomes the fields in the first array. It is a lot easier to understand the code with Employees[2].Host thant it woud be with Employees[2][4].
ASKER CERTIFIED SOLUTION
Avatar of theHollow
theHollow
Flag of Norway 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
I can add that there is a .ToArray()-method on the List. So you can create back to arrays whenever you like with this.Employees.ToArray() or this.Amployees.ToArray<EmployeeInfo>()
Avatar of it_saige
TheHollow is spot on.  Using a generic is preferrable to using an array.  The List<> is one of these generics that is available by using the System.Collection.Generic namespace.

http://www.dotnetperls.com/list
http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

-saige-
Avatar of thaburner

ASKER

That worked perfect!!! Thanks.