Reputation: 205
I have been trying to find the awnser to this for a while. I am converting a site from php to asp. I have an array like this.
$students = array(
array(
"id" => "1",
"name" => "John",
"group" => "A"
),
array(
"id" => "2",
"name" => "Joe",
"group" => "A"
),
array(
"id" => "3",
"name" => "Derp",
"group" => "B"
),
);
foreach($student as $slacker){
//use the data
}
Is there any alternative that even comes close to this with asp?
Upvotes: 1
Views: 1552
Reputation: 148150
You can make a class and use generic list to hold the array of your class type.
public class YourGroup
{
public string id { get; set; };
public string name { get; set; };
public string group { get; set; };
}
List<YourGroup> lstGroup = new List<YourGroup>();
lstGroup.Add(new YourGroup(id ="1", name="Jon", group="A1"));
lstGroup.Add(new YourGroup(id ="2", name="Jon", group="A2"));
lstGroup.Add(new YourGroup(id ="3", name="Jon", group="A3"));
string idOfFirst lstGroup[0].id;
Upvotes: 2
Reputation: 7923
You might be looking for what is called a dictionary. While its not quite deep nested like $myArray['foo']['bar'] the dictionary will allow you to stuff like
Dictionary<int, MyObject> myDictionary = new Dictionary<int, MyObject>();
where your MyObject holds the following object
class MyObject
{
public string name;
public string group;
}
as a result you can traverse it as such
MyObject foo = new MyObject();
foo.name = "tada!";
foo.group = "foo";
myDictionary.add(1, foo);
foreach (MyObject obj in Dictionary.Values)
{
//Do Stuff
}
a completely string associated array would be
Dictionary<string, string> myDictionary = new Dictionary<string, string>();
string something = myDictionary["value"];
Upvotes: 0