• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

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

0
thaburner
Asked:
thaburner
1 Solution
 
Jacques Bourgeois (James Burger)Commented:
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].
0
 
theHollowCommented:
Hello
I know it's not a direct answer to the question...

But do you need to have an array inside an array?

Would it be acceptable to create a new class, f.ex. named "EmployeeInfo", which holds five public properties.

Example:
public EmployeeInfo
{
    public Int32 Id { get; set; }
    public String Host { get; set; }
    public String EmployeeId { get; set; }
    public Int32 Permission { get; set; }
    public String Value { get; set; }
}

Open in new window

And then make a list from this:

public List<EmployeeInfo> Employees = null;

this.Employees = new List<EmployeeInto>();
this.Employees.Add(new EmployeeInto() { Id = 1, Host = "Someting", EmployeeId = "123" });
this.Employees.Add(new EmployeeInto() { Id = 2, Host = "Someting else", EmployeeId = "456" });

Open in new window

You can also add to this list from a file or from a database.
You can use the list as ItemsSource for a control.
And you can loop it with a foreach-loop:

foreach (EmployeeInfo employee in this.Employees)
{
    Debug.WriteLine(employee.Id.ToString() + " - " + employee.Name);
}

Open in new window


I'd go for this unless there is a specific reason or cause to use arrays in arrays.

Hope it helped :-)
0
 
theHollowCommented:
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>()
0
 
it_saigeDeveloperCommented:
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-
0
 
thaburnerAuthor Commented:
That worked perfect!!! Thanks.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now