Tinus,
I think there are some other alternatives you can choose to deal with
your problem more efficently. However, just to answer your specific
question about being able to use statements like:
test["Age"][1], test["Street"][0], etc, I can give you one way to do
it.
Basically what you need is to provide an indexer with string and int
accessor type to your class. Here is a sample class:
class Contact
{
private ArrayList name; // "Name"
private ArrayList street; // "Street"
private ArrayList age; // "Age"
public Contact()
{
name = new ArrayList();
street = new ArrayList();
age = new ArrayList();
}
public object this[string colName, int index]
{
get
{
ArrayList result;
switch (colName)
{
case "Name":
result = name;
break;
case "Street":
result = street;
break;
case "Age":
result = age;
break;
default:
result = null;
break;
}
if (result != null)
{
try
{
return result[index];
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
return null;
}
set
{
ArrayList temp;
switch (colName)
{
case "Name":
temp = name;
break;
case "Street":
temp = street;
break;
case "Age":
temp = age;
break;
default:
temp = null;
break;
}
if (temp != null)
{
if (index >= temp.Count)
{
// Add new item
temp.Add(value);
}
else
{
try
{
temp[index] = value;
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
}
}
}
}
To use the class:
class IndexerSample
{
static void Main(string[] args)
{
Contact test = new Contact();
// first row
test["Name",0] = "Tinus";
test["Street",0] = "Street 1";
test["Age",0] = 15;
// second row
test["Name",1] = "Bert";
test["Street",1] = "Street 2";
test["Age",1] = 18;
//Display the contacts
for(int i=0;i<2;i++)
{
Console.Write(test["Name",i] + " ");
Console.Write(test["Street",i] + " ");
Console.WriteLine(test["Age",i]);
}
Console.WriteLine("Press <ENTER> to continue...");
Console.ReadLine();
}
}
Hope it helps.
--
Ricky Lee
============================================================
^o^ "When all doors are closed, God will open a Windows" ^o^
============================================================
Well, this is graphically what I want:
Name Street Age
----------------------------
Tinus Street 1 15
Bert Street 2 18
And then I want to get these values using a statement like this:
test["Age"][1] would result in "18" and
test["Street"][0] would result in "Street 1" etc.
A Hashtable can't do that right? And as far as I see a sorted list can't do
that also....
Tinus
Cor Ligthert said: