Results 1 to 7 of 7

Thread: Array of Structs

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2012
    Posts
    27

    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?

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: Array of Structs

    vbnet Code:
    1. Dim BtnStruct(15) As ButtonStruct
    The above should be at the form level, you declared it in Form_Load

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2012
    Posts
    27

    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?

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    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:
    1. Public Class buttonStruct
    2.  
    3.         Dim x() As Boolean
    4.         Dim y() As Boolean
    5.         Dim z As Boolean
    6.         Dim q As Integer
    7.  
    8.         Public Sub New()
    9.  
    10.             Me.x = New Boolean(32) {}
    11.             Me.y = New Boolean(32) {}
    12.             Me.z = New Boolean
    13.             Me.q = New Integer
    14.         End Sub
    15.  
    16.     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.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    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

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,348

    Re: Array of Structs

    Quote Originally Posted by Shaggy Hiker View Post
    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

  7. #7
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,538

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width