Solved

Read/Write Array in Array

Posted on 2011-09-23
5
230 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 32

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
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:…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now