Solved

Read/Write Array in Array

Posted on 2011-09-23
5
243 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 34

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

717 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