-
Mar 6th, 2012, 04:11 PM
#1
Thread Starter
Junior Member
Array of Structs
I am trying to create and initialize an array of structs of a fixed size. Each struct will be populated when a button is clicked. Here is an example of my struct:
public structure buttonStruct
dim x() as Boolean
dim y() as Boolean
dim z as Boolean
dim q as integer
Public Sub Initialize()
Me.x = New Boolean(32) {}
Me.y = New Boolean(32) {}
Me.z = New Boolean
Me.q = New integer
End Sub
end structure
In my form_load function I am declaring the array of structs and:
Dim BtnStruct(15) As ButtonStruct
BtnStruct(0).Initialize()
BtnStruct(1).Initialize()
.
.
BtnStruct(15).Initialize()
Whenever a specific button is clicked, I am trying to collect data and store it in the respective struct and so for example, if button 1 is clicked then I will store the data associated with button 1 into BtnStruct(0). The position of the button is stored in a public variable called BtnNum. If I attempt to store information in the struct inside another function as follows, I receive an error
BtnStruct(BtnNum).x(pos - 1) = temp
"btnstruct is not declared. it may not be inaccessible due to its protection level"
Any ideas?
-
Mar 6th, 2012, 04:15 PM
#2
Re: Array of Structs
vbnet Code:
Dim BtnStruct(15) As ButtonStruct
The above should be at the form level, you declared it in Form_Load
-
Mar 6th, 2012, 04:33 PM
#3
Thread Starter
Junior Member
Re: Array of Structs
Thanks for the response. I knew it was something silly. Do you have any recommendations regarding how to initialize all of the elements within the struct array? Is my approach okay as far as using an Initialize function:
BtnStruct(0).Initialize()
BtnStruct(1).Initialize()
Is it better to initialize each struct element when the data is going to be stored in the struct instead of initializing all of the struct elements in the form_load?
-
Mar 6th, 2012, 05:04 PM
#4
Re: Array of Structs
Thats about the only way you can initialize a structure, however if you declared it as a class instead you could do this:-
vbnet Code:
Public Class buttonStruct Dim x() As Boolean Dim y() As Boolean Dim z As Boolean Dim q As Integer Public Sub New() Me.x = New Boolean(32) {} Me.y = New Boolean(32) {} Me.z = New Boolean Me.q = New Integer End Sub End Class
Note that classes are reference types and structures are value types so if you decide to change it, be aware of this important difference.
-
Mar 6th, 2012, 05:39 PM
#5
Re: Array of Structs
I was going to write that your code won't work because of this line:
BtnStruct(BtnNum).x(pos - 1) = temp
but it actually should, though only because x is a reference type (an array). If you try something like that with z or q, then it will fail, because when this part runs:
BtnStruct(BtnNum)
it will return a copy of the structure, and the rest of the line makes changes to the copy and not to the instance that is in the array. However, since you are making changes to a reference type in the copy, then it will work because the copy holds a copy of the reference, not a copy of the array. Otherwise, if you use structures in that way, what you have to do is this:
Code:
Dim tempButtonStruct = BtnStruct(BtnNum)
'make your changes on tempButtonStruct
BtnStruct(BtnNum) = tempButtonStruct
In other words, make a copy, change the copy, then save the copy back into the array, thereby replacing the structure that is in there.
My usual boring signature: Nothing
-
Mar 6th, 2012, 08:49 PM
#6
Re: Array of Structs
Originally Posted by Shaggy Hiker
when this part runs:
BtnStruct(BtnNum)
it will return a copy of the structure.
I used to think that too but, as it turns out, arrays are a little different to what you might expect in that indexing gives you the actual value, not a copy. Try this code to see that in action:
Code:
Public Class Form1
Private Structure X
Public Y As Integer
End Structure
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim a(0) As X
'This will create a copy.
Dim b = a(0)
'This won't.
a(0).Y = 100
MessageBox.Show(a(0).Y.ToString())
MessageBox.Show(b.Y.ToString())
End Sub
End Class
-
Mar 6th, 2012, 10:07 PM
#7
Re: Array of Structs
Where you get into the problem with copy vs actual structure is with iterators ... for each and the like.... stepping through an array with index though should be safe.
-tg
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|