PROPERTY STATEMENTS WITH ARRAYS

WHAT IS THE CORRECT SYNTAX FOR CREATING A PROPERTY WITH DIMENSIONED ARRAYS. I CAN'T FIND ANYTHING LIKE THIS IN DOCUMENTATION.
[127 byte] By [NUMBSCULL] at [2007-12-16]
# 1
Exactly as you would expect, except that you cannot specify the number of elements in each dimension. This makes sense because you are declaring the type of something, rather than creating a specific object.

Public Property MyArray() As String(,)
Get
End Get
Set(ByVal Value As String(,))
End Set
End Property


jmcilhinney at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2

Hello JMcilhinney
I made some modifications to the code you wrote for me. I took out the "," delimeter in the expressions because I'm working with arrays of rank 1. I believe the single "," makes the rank 2. I added a local variable because documentation on get procedure recommended that. In the following code an error ocorrs in the shaded violet color. The error says "Object reference not set to an instance of an object."

Module Module1

Private local_array As Integer()

Public Property array() As Integer()

Get

Return local_array

End Get

Set(ByVal value As Integer())

local_array = value

End Set

End Property

Sub Main()

Dim i As Integer = 0

For i = 1 To 10

array(i) = 10 * i

Next

End Sub

End Module


NUMBSCULL at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3
I'm not sure why you would want to make a property out of the array when it's in the same module, it's shares the same scope as your functions, no real need to do it.

However, the issue isn't you property code. It's in the sub Main()
You have to redim your array before you can use it.



Sub Main()
Dim i As Integer = 0

ReDim
array(9)

For i = 0 To array.GetUpperBound(0)
array(i) = 10 * i
Next
End Sub

I've changed your code for speed improvement.
You loop was from 1 to 10. Arrays start at base 0.
Redim Array(9) will create a 10 item array.
i changed For i = 0 To array.GetUpperBound(0)
becaue when you have a for/loop there's a small performance increase to use the .GetUpperBound(rank) function, as the compiler knows you will not change the dimensions of the array during the loop.
Hope this help!

Dustin

Dustin_H at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4

hello Dustin

The program works fine with added code.
Thanks !
The reason for the property statements in the module are to allow me to access these arrays from any form in my application. I was told this was one way to do this.

NUMBSCULL at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...