0

I'm having a hard time displaying rectangles from an array. Here's the code:

Dim recs(9) As PowerPacks.RectangleShape
Dim canvas As New Microsoft.VisualBasic.PowerPacks.ShapeContainer

   Private Sub Spi_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim sz As Integer = 350
    canvas.Parent = Me

    recs(0).Height = sz
    recs(0).Width = sz
    recs(0).Left = 20
    recs(0).Top = 20
    recs(0).Parent = canvas
    recs(0).FillColor = Color.Green
    recs(0).FillStyle = PowerPacks.FillStyle.Solid

End Sub

There are no syntax or runtime errors. I can't figure out what is going on. If I try to make a rectangle alone it will show it but when I'm making an array of them, it shows nothing. If I debug it, when the next statement to be executed is any line of code that uses the array, it justs ignores it and moves on, meaning that it will ignore all my commands that use the "recs(0)". Why??? Any help is appreciated. Thanks.

EDIT: Ok guys. Thanks for your help.

CosmaOne
  • 21
  • 5
  • 2
    There is a runtime error, your code crashes with a NullReferenceException. You just can't see it because of a Windows bug, made a lot worse by putting code that belongs in the constructor into the Load event handler instead. You are not initializing the array elements, recs(0) = new PowerPacks.RectangleShape is required. – Hans Passant May 06 '14 at 23:16

1 Answers1

0

As has been mentioned, your problem is, the array isn't initialized, and since you can't initialize an array with New you'll either have to initialize each element or switch to a different collection, like List:

Dim recs(9) As PowerPacks.RectangleShape
Dim canvas As New Microsoft.VisualBasic.PowerPacks.ShapeContainer

Private Sub Spi_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim sz As Integer = 350
    canvas.Parent = Me
    recs(0) = New PowerPacks.RectangleShape(20, 20, sz, sz)
    recs(0).Parent = canvas
    recs(0).FillColor = Color.Green
    recs(0).FillStyle = PowerPacks.FillStyle.Solid
End Sub


Dim recs As New List(Of PowerPacks.RectangleShape)
tinstaafl
  • 6,908
  • 2
  • 15
  • 22
  • Right, but you didn't fix the other problem that was mentioned: performing initialization in the `Load` event handler, rather than the constructor. If he'd done that in the first place, the problem would have been obvious. – Cody Gray - on strike May 07 '14 at 05:09