By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,671 Members | 1,393 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,671 IT Pros & Developers. It's quick & easy.

Something similar like a DataTable?

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Tinus,

Not simular however probably accoording to your needs.

Hashtable
http://msdn.microsoft.com/library/de...classtopic.asp

Sortedlist
http://msdn.microsoft.com/library/de...classtopic.asp
I hope this helps,

Cor
Nov 16 '05 #2

P: n/a
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" <no************@planet.nl> wrote in message
news:eX**************@TK2MSFTNGP14.phx.gbl...
Tinus,

Not simular however probably accoording to your needs.

Hashtable
http://msdn.microsoft.com/library/de...classtopic.asp

Sortedlist
http://msdn.microsoft.com/library/de...classtopic.asp
I hope this helps,

Cor

Nov 16 '05 #3

P: n/a
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
Nov 16 '05 #4

P: n/a
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" <no****************@xxx.karssemeijer.com.noadv.xxx > wrote in message
news:eP**************@tk2msftngp13.phx.gbl...
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" <no************@planet.nl> wrote in message
news:eX**************@TK2MSFTNGP14.phx.gbl...
Tinus,

Not simular however probably accoording to your needs.

Hashtable
http://msdn.microsoft.com/library/de...classtopic.asp
Sortedlist
http://msdn.microsoft.com/library/de...classtopic.asp

I hope this helps,

Cor



Nov 16 '05 #5

P: n/a
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
Nov 16 '05 #6

P: n/a
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^
================================================== ==========
On Fri, 4 Mar 2005 12:45:54 +0100, "Tinus"
<no****************@xxx.karssemeijer.com.noadv.xxx > wrote:
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" <no************@planet.nl> wrote in message
news:eX**************@TK2MSFTNGP14.phx.gbl...
Tinus,

Not simular however probably accoording to your needs.

Hashtable
http://msdn.microsoft.com/library/de...classtopic.asp

Sortedlist
http://msdn.microsoft.com/library/de...classtopic.asp
I hope this helps,

Cor


Nov 16 '05 #7

P: n/a
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" <no****************@xxx.karssemeijer.com.noadv.xxx > wrote in message
news:eP****************@tk2msftngp13.phx.gbl...
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" <no************@planet.nl> wrote in message
news:eX**************@TK2MSFTNGP14.phx.gbl...
Tinus,

Not simular however probably accoording to your needs.

Hashtable
http://msdn.microsoft.com/library/de...classtopic.asp

Sortedlist
http://msdn.microsoft.com/library/de...classtopic.asp
I hope this helps,

Cor


Nov 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.