Array as little table

A

Arjen

Hi,

I want to use an array as a little table. I don't know how to declare
this in C# and I also don't know how to find a record in the array
(based on id field). Hope somebody can help me.

Here is the array/table structure I'm looking for.
id, name, address
value, value, value
value, value, value
value, value, value
value, value, value, etc.

I tried all kind of formulations string[], string[,], but I don't get
it.

Hope someone helps, thanks!
 
P

Peter Duniho

I want to use an array as a little table. I don't know how to declare
this in C# and I also don't know how to find a record in the array
(based on id field). Hope somebody can help me.

Here is the array/table structure I'm looking for.
id, name, address
value, value, value
value, value, value
value, value, value
value, value, value, etc.

I tried all kind of formulations string[], string[,], but I don't get
it.

Well, I think the most straightforward way would be to declare a structure
or class that holds one record's worth of the "id", "name", and "address"
values and then make a regular array of those objects.

If all of your values are the same type (e.g. string) I suppose you could
make a two-dimensional array, where one dimension is three (given the
three fields you indicated) and the other dimension is the number of
records. But I don't think that's a very good design.

Pete
 
M

Marc Gravell

Rather than arrays, how about (using C# 3 syntax, but all possible in
C# 2):

using System.Collections.Generic;

class Foo // your data entity...
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public override string ToString()
{
return Name;
}
}

class Program
{
static void Main()
{
// create some data
List<Foo> myData = new List<Foo>();
myData.Add(new Foo { Id = 1, Name = "Fred", Address =
"Whatever" });
myData.Add(new Foo { Id = 2, Name = "Barney", Address =
"Whatever" });

// find record 2
Foo foo = myData.Find(x => x.Id == 2);
}
}
 
A

Arjen

Rather than arrays, how about (using C# 3 syntax, but all possible in
C# 2):

using System.Collections.Generic;

class Foo // your data entity...
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public override string ToString()
    {
        return Name;
    }

}

class Program
{
    static void Main()
    {
        // create some data
        List<Foo> myData = new List<Foo>();
        myData.Add(new Foo { Id = 1, Name = "Fred", Address =
"Whatever" });
        myData.Add(new Foo { Id = 2, Name = "Barney", Address =
"Whatever" });

        // find record 2
        Foo foo = myData.Find(x => x.Id == 2);
    }



}- Tekst uit oorspronkelijk bericht niet weergeven -

- Tekst uit oorspronkelijk bericht weergeven -

Peter Duniho, Marc Gravell,

Thanks for the tip. I will use objects.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top