Solved

Read/Write Array in Array

Posted on 2011-09-23
5
238 Views
Last Modified: 2013-12-17
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
Comment
Question by:thaburner
5 Comments
 
LVL 40
ID: 36588983
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
 
LVL 4

Accepted Solution

by:
theHollow earned 500 total points
ID: 36589035
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
 
LVL 4

Expert Comment

by:theHollow
ID: 36589068
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
 
LVL 33

Expert Comment

by:it_saige
ID: 36589427
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
 

Author Closing Comment

by:thaburner
ID: 36599584
That worked perfect!!! Thanks.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

808 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