Something similar like a DataTable?

  • Thread starter Thread starter Tinus
  • Start date Start date
T

Tinus

Hello all,

I was wondering... Is there a way to create a Array so that I can call a
value with a name i.s.o. a index number? Something similar like a DataTable.

This is what I want: Declare a array of some sorts, private string[,] test;
....
I don't want to get the value by saying: test[0,1] but test["name"][1]

Can this only be done with a DataTable? But DataTables are for databases
right?

Hope someone can shed a light on this for me.

Thanks,
Tinus
 
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
 
Tinus,

I use forever the datatable however some people have something against it.
It is not only for databases.

To give you a sample very rougly typed in this message without any checking

\\\
DataTable tb = new DataTable("myTable");
tb.Columns.Add("Name",GetType.Type("System.String"));
tb.Columns.Add("etc

tb.Rows.Add(tb.NewRow);
tb.Rows[tb.Rows.Count-1]["Name"] = "Tinus";
///

I hope this helps?

Cor
 
Here is another option. Not multidimensional array, but more OO and
conceptually a lot easier imo. You could also create a collection class
that "contains" the ArrayList to give strong typing and not have to do the
external cast (internal cast still required). You could also use a generic
collection in fw2.0.

public class Item
{
private string name;
private string street;
private int age;

public Item(string name, string street, int age)
{
this.name = name;
this.street = street;
this.age = age;
}
public string Name
{
get { return name; }
set { name = value; }
}
public string Street
{
get { return street; }
set { street = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
}

private void button20_Click(object sender, System.EventArgs e)
{
ArrayList al = new ArrayList();
Item i1 = new Item("Bill", "123 Street", 30);
Item i2 = new Item("John", "345 Street", 25);
al.Add(i1);
al.Add(i2);

// Enum all.
foreach(Item i in al)
{
Console.WriteLine("{0} {1} {2}", i.Name, i.Street, i.Age);
}

// Get one.
string name = ((Item)al[0]).Name;
Console.WriteLine("Name at 0:"+name);
}

--
William Stacey, MVP
http://mvp.support.microsoft.com

Tinus said:
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:
 
William,

I don't discuss that it is not a good option.

However some people give me the idea that it is only OO when build your
dataclasses yourself.

I can me not imagen a more OO class with all its aspect than the Dataset.

Therefore why are you writing this sentence "but more OO"?

Cor
 
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:
 
Thank you all for the given solutions. I'm going to take a look at them
later this day but they sound promissing.

Tinus

Tinus said:
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:
 
Back
Top