Unsolved: property Cells[row, col] (2 dimensional array as string)

J

jodleren

Hi all

I am still looking for a solution for a grid - I have the design
ready, but I need to set and store data:

In Delphi, there is a TStringGrid.Cells

property Cells[ACol, ARow: Integer]: string read GetCells write
SetCells;

where I can go: sg.Cells[1, 1] = "text"

Now, for C# I need so have it like:

public string Cells[x,y]
{
get....set...
}

and I need to store it - a List<> is good, but I need it 2 dimensional

WBR
Sonnich
 
W

Willem van Rumpt

Hi all

I am still looking for a solution for a grid - I have the design
ready, but I need to set and store data:

In Delphi, there is a TStringGrid.Cells

property Cells[ACol, ARow: Integer]: string read GetCells write
SetCells;

where I can go: sg.Cells[1, 1] = "text"

Now, for C# I need so have it like:

public string Cells[x,y]
{
get....set...
}

and I need to store it - a List<> is good, but I need it 2 dimensional

I'm not sure I understand what the problem is, but, judging from the
last sentence, it looks like you need a class portraying a row, where
the row class has columns.

The problem and the solution are the same Delphi and C#, only the
declaration of the property differs:

in Delphi you would use

property Cells[row, column: Integer]
read <resolve row and column in the store>
write <resolve row and column in the store>

in C# you use

public Cell this[int row, int column]
{
get { return <resolve row and column in the store>; }
set { <resolve row and column in the store> = value; }
}


Note that C# does not allow named indexed properties (some .NET
languages do, VB.NET, and Oxygene (Delphi Prism), if I'm not mistaken);
The only way to declare an indexed property in C# is in the way above.
 
W

Willem van Rumpt

property Cells[ACol, ARow: Integer]: string read GetCells write
SetCells;

where I can go: sg.Cells[1, 1] = "text"

Now, for C# I need so have it like:

public string Cells[x,y]
{
get....set...
}

in Delphi you would use

property Cells[row, column: Integer]
read <resolve row and column in the store>
write <resolve row and column in the store>

in C# you use

public Cell this[int row, int column]
{
get { return <resolve row and column in the store>; }
set { <resolve row and column in the store> = value; }
}

I just saw your other post ("List of List?"). Peter Duniho gave you the
answer in his reply to your reply to Jeff Johnson.

Could you describe more elaborately what problems you still have?
I worked (and on rare occasions still work) with Delphi, maybe you can
show how you would solve the problem in Delphi, and what exactly it is
you can't find a match in C# for.
 
J

jodleren

On 14-Mar-11 17:45, jodleren wrote:
property Cells[ACol, ARow: Integer]: string read GetCells write
SetCells;
where I can go: sg.Cells[1, 1] = "text"
Now, for C# I need so have it like:
public string Cells[x,y]
{
get....set...
}

in Delphi you would use
property Cells[row, column: Integer]
read <resolve row and column in the store>
write <resolve row and column in the store>
in C# you use
public Cell this[int row, int column]
{
get { return <resolve row and column in the store>; }
set { <resolve row and column in the store> = value; }
}

I just saw your other post ("List of List?"). Peter Duniho gave you the
answer in his reply to your reply to Jeff Johnson.

Could you describe more elaborately what problems you still have?
I worked (and on rare occasions still work) with Delphi, maybe you can
show how you would solve the problem in Delphi, and what exactly it is
you can't find a match in C# for.

I think I got it -

instead of

component.property[1, 2] =

I should use

component[1, 2] =

but I still need to have an array to keep my data

My reason for skipping "list of list" is that it was an old post,
which is not read anymore. Secondly the relation to the property is
also a part of it. And I think I understand the property issue now. I
am still new to certain things in C#

So:

List<string> _data;
property string this[int row, int col]
{
get
{
return _data[int, col];
}
set
{
_data[int, col] = value;
refresh();
}
}

or how do I create a list with double indexing?
 
W

Willem van Rumpt

I think I got it -

instead of

component.property[1, 2] =

I should use

component[1, 2] =

Yes, C# doesn't know named index properties. This is the way to access them.
but I still need to have an array to keep my data

My reason for skipping "list of list" is that it was an old post,
which is not read anymore. Secondly the relation to the property is
also a part of it. And I think I understand the property issue now. I
am still new to certain things in C#

So:

List<string> _data;
property string this[int row, int col]
{
get
{
return _data[int, col];
}
set
{
_data[int, col] = value;
refresh();
}
}

or how do I create a list with double indexing?

Well, a List in C# is still just that, a list, and for practical
purposes similar to a TList in Delphi (and since you want to store
strings, a TStringList).

If you don't want to use an array to store the strings, you'll have to
setup classes that define or can be assembled in to a two-dimensional
structure yourself.

One simple, naive implementation would be:

public class Column
{
public string Value { get; set; }
}

public class Row
{
private List<Column> _columns = new List<Column>();
public Column this[int index]
{
get { return _columns[index]; }
set { _columns[index].Value = value; }
}
}

public class StringGrid
{
private List<Row> _rows = new List<Row>();
public string this[int row, int column]
{
get { return _rows[row][column].Value; }
set { _rows[row][column].Value = value; }
}
}

This example doesn't contain any bounds checks or other validation (but
may contain typos ;) ), but it shows the basic principle.

It's the same problem you'll have in Delphi, if you don't want to use an
multidimensional array as the storage for your strings: You'll have to
define a structure of your own.
 
W

Willem van Rumpt

It's not clear to me what the above provides above simply using "string"
where your code uses "Column".

To give a simple implementation example containing a clear row - column
concept. To my mind, it does a better job at bringing the point home
than providing an implementation of a list of rows, where each row holds
a string, or other completely valid alternatives. From what I can tell,
his problem is setting up the data structure in the first place. Any
example that makes it less abstract should benefit the OP.
public class Row
{
private List<Column> _columns = new List<Column>();
public Column this[int index]
{
get { return _columns[index]; }
set { _columns[index].Value = value; }
}
}

public class StringGrid
{
private List<Row> _rows = new List<Row>();
public string this[int row, int column]
{
get { return _rows[row][column].Value; }
set { _rows[row][column].Value = value; }
}
}

Note that it doesn't even populate the List<T> instances. Without code
to do that, it won't work at all, even if the incorrect property
declaration is fixed (in C#, the "property" keyword is not used).

Where did I use an incorrect property declaration?
Yes. And in fact, a two-dimensional array meets every requirement that
the OP has stated so far.

Indeed.

Unfortunately, he has refused to provide any
additional information that would explain why he isn't just using one of
those, rather than trying to shoe-horn List<T> into his design. Fact is,
given the information provided so far, it could just as easily be the
case that a Dictionary<int, Dictionary<int, string>> is the appropriate
data structure.

Yup. There are not /that/ many options out there that would fit the
structure. That's why I suggested to him to write what he would do in
Delphi, so we could provide a C# concept that maps to his Delphi code
(which actually all suggestions so far, including the ones in the other
thread, do).
 

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