Indexer to Jagged array

A

almurph

Hi everyone,


I am trying to create an indexer to a jagged array but can't seem to
be able to do it. I am trying the following:


**** BEGIN SUSPECT JAGGED ARRAY INDEXER CODE*****


private int[][] AttributeValue;

public int this[int row, int column]
{
get
{
return AttributeValue[row][column];
}
set
{
AttributeValue[row][column] = value;
}
}


**** END SUSPECT JAGGED ARRAY INDEXER CODE*****


I'm not sure about it. Would appreciate any comments/suggestions/
corrections/code-samples that you may be able to offer.

Thanks,
Al
 
A

Alberto Poblacion

I am trying to create an indexer to a jagged array but can't seem to
be able to do it. I am trying the following [...]

Your code should work... on the condition that every row and column that
you try to reach through the indexer is already allocated, since it doesn't
contain any checks to verify that this is true or to allocate memory to the
arrays if it isn't.
Note that assigning a value to a specific row/column, as you are doing
in the setter, does *not* create that element; it requires that *both*
arrays (the main one (row) and the "jagged" (column) one) be already
dimensioned to contain the cell that you are accessing.
 
A

almurph

Hi Alberto,

Thanks for your help. Can you give me an example of what you mean
please? I'm kind of new to C# and still learning the ropes.

Cheers & thanks again,
Al

Alberto said:
I am trying to create an indexer to a jagged array but can't seem to
be able to do it. I am trying the following [...]

Your code should work... on the condition that every row and column that
you try to reach through the indexer is already allocated, since it doesn't
contain any checks to verify that this is true or to allocate memory to the
arrays if it isn't.
Note that assigning a value to a specific row/column, as you are doing
in the setter, does *not* create that element; it requires that *both*
arrays (the main one (row) and the "jagged" (column) one) be already
dimensioned to contain the cell that you are accessing.
 
S

Stefan L

Hi Al,

your code should work, but it depends on how you want callers to access
the values. For callers it looks like the access is multidimensional,
not a jagged array. An alternative way for an indexer is to return the
complete nested array. In this version you would only need a get
accessor, because the set operation is directly done in the array (see
below).

// indexer for access with object[row, col];
public int this[int row, int column]
{
get
{
return AttributeValue[row][column];
}
set
{
AttributeValue[row][column] = value;
}
}

// indexer for access with object[row][col];
// note: you need only a getter here.
public int[] this[int row]
{
get
{
return AttributeValue[row];
}
}


HTH,
Stefan
 
A

almurph

Hi Al,

your code should work, but it depends on how you want callers to access
the values. For callers it looks like the access is multidimensional,
not a jagged array. An alternative way for an indexer is to return the
complete nested array. In this version you would only need a get
accessor, because the set operation is directly done in the array (see
below).

         // indexer for access with object[row, col];
         public int this[int row, int column]
         {
             get
             {
                 return AttributeValue[row][column];
             }
             set
             {
                 AttributeValue[row][column] = value;
             }
         }

         // indexer for access with object[row][col];
         // note: you need only a getter here.
         public int[] this[int row]
         {
             get
             {
                 return AttributeValue[row];
             }
         }

HTH,
    Stefan

(e-mail address removed) schrieb:


Hi everyone,
   I am trying to create an indexer to a jagged array but can't seem to
be able to do it. I am trying the following:
**** BEGIN SUSPECT JAGGED ARRAY INDEXER CODE*****
private int[][] AttributeValue;
public int this[int row, int column]
{
   get
        {
           return AttributeValue[row][column];
        }
        set
        {
           AttributeValue[row][column] = value;
        }
}
**** END SUSPECT JAGGED ARRAY INDEXER CODE*****
   I'm not sure about it. Would appreciate any comments/suggestions/
corrections/code-samples that you may be able to offer.
Thanks,
Al- Hide quoted text -

- Show quoted text -

Hey thanks Stefan - I see what you mean,

Another boring question: Is it possible to have a static
'version' of this indexer. I think I need this, as once I populate it
I need it accessible to other classes...

Thanks yet again,
Al.
 
S

Stefan L

Hi Al,

I actually never had use for it, so I don't know...
Hmm, I just tried it, and no, static modifier for an indexer is not allowed.

But it makes sense in a certain way, the syntax is already occupied by
an array declaration...: Type[int]

HTH,
Stefan
 
A

almurph

Hi Al,

I actually never had use for it, so I don't know...
Hmm, I just tried it, and no, static modifier for an indexer is not allowed.

But it makes sense in a certain way, the syntax is already occupied by
an array declaration...: Type[int]

HTH,
    Stefan

(e-mail address removed) schrieb:


Hey thanks Stefan - I see what you mean,
            Another boring question: Is it possible to havea static
'version' of this indexer. I think I need this, as once I populate it
I need it accessible to other classes...
Thanks yet again,
Al.- Hide quoted text -

- Show quoted text -

Thanks a million Stefan. Al
 
P

Pavel Minaev

Hi everyone,

        I am trying to create an indexer to a jagged array but can't seem to
be able to do it. I am trying the following:

**** BEGIN SUSPECT JAGGED ARRAY INDEXER CODE*****

private int[][] AttributeValue;

public int this[int row, int column]
{
        get
        {
                return AttributeValue[row][column];
        }
        set
        {
                AttributeValue[row][column] = value;
        }

}

**** END SUSPECT JAGGED ARRAY INDEXER CODE*****

        I'm not sure about it. Would appreciate any comments/suggestions/
corrections/code-samples that you may be able to offer.

Just out of curiosity, why don't you use a plain 2-dimensional array
rather than jagged one for the backing storage? i.e. int[,] rather
than int[][] - you seem to want a square matrix, and that'll give you
just that, and will result in only one memory block allocated for the
whole array.
 
A

Alberto Poblacion

Thanks for your help. Can you give me an example of what you mean
please? I'm kind of new to C# and still learning the ropes.

I was thinking of something along these lines:

private int[][] AttributeValue;
public int this[int row, int column]
{
get
{
VerifyIndexes(row, column);
return AttributeValue[row][column];
}
set
{
VerifyIndexes(row, column);
AttributeValue[row][column] = value;
}
}
private void VerifyIndexes(int row, int column)
{
if (AttributeValue==null)
throw new Exception("AttributeValue has not been initialized (you did
not do AttributeValue=new int[][numberOfRows])");
if (row<0 || row>=AttributeValue.Length)
throw new Exception("row is out of range");
if (AttributeValue[row]==null)
throw new Exception("AttributeValue["+row+"] has not been initialized
(you did not do AttributeValue["+row+"]=new int[numberOfColumns])");
if (column<0 || column>=AttributeValue[row].Length)
throw new Exception("This row of the jagged array has not been
allocated enough columns");
}


The text that I wrote for the exceptions should tell you the operations
that you need to do on your jagged array before accessing the values through
the indexer.
 

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