?
Solved

Read/Write Array in Array

Posted on 2011-09-23
5
Medium Priority
?
246 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 2000 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

AWS Certified Solutions Architect - Associate

This course has been developed to provide you with the requisite knowledge to not only pass the AWS CSA certification exam but also gain the hands-on experience required to become a qualified AWS Solutions architect working in a real-world environment.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

770 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