Accessing Arrays

joe_pool_is

Contributor
Joined
Jan 18, 2004
Messages
507
Location
Longview, TX [USA]
I've got a 8x8 array.

Each element of the array will contain an object that consists of a letter, a color, and a double value.

Creating and working with arrays in my class is not the hard part.

The hard part is how do I make them publicly accessible outside of my class?

In other words, how would I write the Get and Set properties for it?

I'm guessing I should not be using the standard 8x8 array, and instead I should be using ...what?

And, how would I implement it?

I'm looking to stay within Microsoft's Best Practices, if I can.

As always, help, links or advice is always appreciated!
 
Hey there

for single arrays you can use this:
Code:
Default Public ReadOnly Property Item(ByVal index As Integer) As Object
        Get
            Return List(index)
        End Get
    End Property

If you want to use an array of more items you can go for an implementation with 'rows' in your array. Create a class or structure which contains a single row of 8 items.
then you can modify it to something like this:
Code:
Default Public Property Item(ByVal index As Integer) As ArrayRow8
        Get
            dim r as new ArrayRow8(.. input data from array8x8(index) .. )
            Return r
        End Get
        Set(byval value as ArrayRow8)
            array8x8(index) = value ' You have to specify an operator overload or copy this per value.
            Return r
        End Get
    End Property

however I must confess I don't know if it's a Microsoft Best Practice, but that's how I solved it in the past. Hope it helps.

~DP
 
That got me started. Thanks!

Along the way, I found this technique that looks really clean:
Code:
public class FormValues
{
  int otherValue;
  string[,] matrix;

  public string this[int X, int Y] {
    get { return matrix[X, Y]; }
    set { matrix[X, Y] = value; }
  }

  public FormValues()
  {
    otherValue = 0;
    matrix = new int[8, 8] { // blah blah blah!
  }

  public int Other { get { return otherValue; } }
}
 
Back
Top