Multidimensional Generics

  • Thread starter Thread starter Chuck Bowling
  • Start date Start date
C

Chuck Bowling

I have a list:

List<RichTextBox> rtb;

That represents an NxM matrix of rtb's. Is there some way to initialize a
generic that will allow me to access the List like a multidimensional array
i.e.;

rtb[2, 3].Text = "some text";
 
Hi,

you can do it like this:

List<List<RichTextBox>> rtb = new List<List<RichTextBox>>();

rtb[2][3].Text = "some text";

Regards,

Wiebe Tijsma
 
"Wiebe Tijsma" <[email protected]> a écrit dans le message de
news: (e-mail address removed)...

| List<List<RichTextBox>> rtb = new List<List<RichTextBox>>();

.... not forgetting :

rtb[2][3] = new RichTextBox(); or similar :-)

| rtb[2][3].Text = "some text";

Joanna
 
Yes, thanks for the addition, lets not forget adding the lists itself as
well :)

List<List<RichTextBox>> rtb = new List<List<RichTextBox>>();
rtb.Add(new List<RichTextBox>());
rtb[0].Add(new RichTextBox());
rtb[0][0].Text = "some text";

Wiebe
 
Q: why not just use a multi-dimensional array of rtbs? However, the
following would wrap it reasonably well, assuming they exist first. At
least, it doesn't appear "jagged".

Marc

public class ArrayWrapper<T> {
private readonly IList<T> data;
private readonly int width;
public ArrayWrapper(IList<T> data, int width) {
this.data = data;
this.width = width;
}
public T this[int x, int y] {
get { return data[GetIndex(x, y)]; }
set { data[GetIndex(x,y)] = value; }
}
private int GetIndex(int x, int y) {
return (y * width) + x;
}
}
 
Great! Thanks Wiebe, Joanna and Marc. :)


Wiebe Tijsma said:
Yes, thanks for the addition, lets not forget adding the lists itself as
well :)

List<List<RichTextBox>> rtb = new List<List<RichTextBox>>();
rtb.Add(new List<RichTextBox>());
rtb[0].Add(new RichTextBox());
rtb[0][0].Text = "some text";

Wiebe
"Wiebe Tijsma" <[email protected]> a écrit dans le message
de news: (e-mail address removed)...

| List<List<RichTextBox>> rtb = new List<List<RichTextBox>>();

... not forgetting :

rtb[2][3] = new RichTextBox(); or similar :-)

| rtb[2][3].Text = "some text";

Joanna
 
Wiebe I'm a little confused on this. Should the code look more like this:

List<List<RichTextBox>> rtb = new List<List<RichTextBox>>();
rtb[0].Add(new List<RichTextBox>());
rtb[0][0].Add(new RichTextBox());
rtb[0][0].Text = "some text";


Wiebe Tijsma said:
Yes, thanks for the addition, lets not forget adding the lists itself as
well :)

List<List<RichTextBox>> rtb = new List<List<RichTextBox>>();
rtb.Add(new List<RichTextBox>());
rtb[0].Add(new RichTextBox());
rtb[0][0].Text = "some text";

Wiebe
"Wiebe Tijsma" <[email protected]> a écrit dans le message
de news: (e-mail address removed)...

| List<List<RichTextBox>> rtb = new List<List<RichTextBox>>();

... not forgetting :

rtb[2][3] = new RichTextBox(); or similar :-)

| rtb[2][3].Text = "some text";

Joanna
 
"Chuck Bowling" <[email protected]> a écrit dans le message
de news: (e-mail address removed)...

| Wiebe I'm a little confused on this. Should the code look more like this:
|
| List<List<RichTextBox>> rtb = new List<List<RichTextBox>>();
| rtb[0].Add(new List<RichTextBox>());
| rtb[0][0].Add(new RichTextBox());
| rtb[0][0].Text = "some text";

Chuck, thanks for filling in the holes, this is perfectly correct, but was
assumed by some of us :-)

Joanna
 

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

Back
Top