arrays and structures

Salat

Well-known member
Joined
Mar 5, 2002
Messages
81
Location
Poland, Silesia
Ive made those structures
Code:
    Public Structure typeObiektStrony
        Dim Left As Integer
        Dim Top As Integer
        Dim Width As Integer
        Dim Height As Integer
        Dim Value As String
        Dim Text As String
        Dim ToolTipText As String
    End Structure
    Public Structure typeStrony
        Dim BackGround As Image
        Dim BakcColor As Color
        Dim Width As Integer
        Dim Height As Integer
        Dim Obiekty() As typeObiektStrony
    End Structure
    Dim dc As typeStrony

when I try to get dc.Obiekty(i).Left the System.NullReferenceException appears. Its probably beacous the number of elements in obiekty array is not specified. I realy dont know how to do this.

Ive searched the forum, msdn and help files... please help me...
 
You need to redim the array Obiekty. Im not sure if the ReDimming part is right - someone can correct me later :)

Code:
    Snipped the structs definitions...
    Dim dc As typeStrony

    ReDim dc.Obiekty(1)
    dc.Obiekty(0).Left = 14
    
    The following will fail because Obiekty is only defined to have one element, (0)
    dc.Obiekty(1).Left = 14

-Nerseus
 
I supose: When You are trying to use array in array then
Code:
redim dc.Obiekty(20)
redim dc.Obiekty(0).CosTam(20)
redim dc.Obiekty(1).CosTam(20)
redim dc.Obiekty(2).CosTam(20)
...
redim dc.Obiekty(20).CosTam(20)
writing just
Code:
redim dc.Obiekty(20).CosTam(20)
wont be enoguht to create array with 21 Obiekty and 21 CosTam in it. Am I right?

And the last question: When I use ReDim in Load event then whole Module will know that there are 21 Obietkty in this array. Right?
 
Yes, you have to Redim each dynamic array that you have... you could do it in a loop:
Code:
Dim LV As Integer
Redim dc.Obiekty(20)
For LV = 0 To 20
  Redim dc.Obiekty(LV).CosTam(20)
Next
And the last question: When I use ReDim in Load event
That depends on where you have declared the original Dim... if you declared it in your main module general area, then the whole module will know, but if you define it in the Load event, then only the Load event will know... but the ReDim can be anywhere... preferably before you start operating with the arrays. :)
 
Back
Top