Results 1 to 6 of 6

Thread: [RESOLVED] Declaring Arrays in Structures

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Resolved [RESOLVED] Declaring Arrays in Structures

    Trying to utlize a code sample I picked up, and I'm having a problem with an Array declared inside a structure.. the code is :
    VB Code:
    1. Public Structure UUID
    2.         Public Data1 As Long
    3.         Public Data2 As Integer
    4.         Public Data3 As Integer
    5.         Public Data4(7) As Byte
    6.     End Structure
    The function that is utilizing it refers to it here :
    VB Code:
    1. With typUUID
    2.                 .Data1 = &H626FC520
    3.                 .Data2 = &HA41E
    4.                 .Data3 = &H11CF
    5.                 .Data4(0) = &HA7
    6.                 .Data4(1) = &H31
    7.                 .Data4(2) = &H0
    8.                 .Data4(3) = &HA0
    9.                 .Data4(4) = &HC9
    10.                 .Data4(5) = &H8
    11.                 .Data4(6) = &H26
    12.                 .Data4(7) = &H37
    13.             End With
    Says "Arrays declared as structure members cannot be declared with initial size" . If I take the initialization out, i can run it, yet the program breaks at the start of the .Data4(0) in the With statement. Have any ideas??

  2. #2
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Re: Declaring Arrays in Structures

    I am quite interested in this too

    I managed to make another structure to make it work...but i dont think this is what you want...

    VB Code:
    1. Public Structure UUID
    2.         Public Data1 As Long
    3.         Public Data2 As Integer
    4.         Public Data3 As Integer
    5.         Public Data4 As Daty
    6.     End Structure
    7.  
    8.     Public Structure Daty
    9.         Friend D1 As Byte
    10.         Friend D2 As Byte
    11.         Friend D3 As Byte
    12.         Friend D4 As Byte
    13.         Friend D5 As Byte
    14.         Friend D6 As Byte
    15.         Friend D7 As Byte
    16.         Friend D8 As Byte
    17.     End Structure
    18.  
    19.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    20.         Dim ken As New UUID
    21.         With ken
    22.             .Data1 = &H626FC520
    23.             .Data2 = &HA41E
    24.             .Data3 = &H11CF
    25.             .Data4.D1 = &HA7
    26.             .Data4.D2 = &H31
    27.             .Data4.D3 = &H0
    28.             .Data4.D4 = &HA0
    29.             .Data4.D5 = &HC9
    30.             .Data4.D6 = &H8
    31.             .Data4.D7 = &H26
    32.             .Data4.D8 = &H37
    33.         End With
    34.     End Sub
    If you find my thread helpful, please remember to rate me

  3. #3
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Re: Declaring Arrays in Structures

    Got it!

    You need to redim ...of course!

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim ken As New UUID
    3.         ReDim ken.Data4(7)
    4.         With ken
    5.             .Data1 = &H626FC520
    6.             .Data2 = &HA41E
    7.             .Data3 = &H11CF
    8.             .Data4(0) = &HA7
    9.             .Data4(1) = &H31
    10.             .Data4(2) = &H0
    11.             .Data4(3) = &HA0
    12.             .Data4(4) = &HC9
    13.             .Data4(5) = &H8
    14.             .Data4(6) = &H26
    15.             .Data4(7) = &H37
    16.  
    17.         End With
    18.     End Sub
    If you find my thread helpful, please remember to rate me

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Declaring Arrays in Structures

    Thx! Redim worked.... simple as that

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Declaring Arrays in Structures

    An array is a reference type object. When you declare an array without an upper bound, it is just like declaring any reference type variable without using the New keyword. When you specify an upper bound, you are in fact creating a new instance of the Array class. These two lines are equivalent:
    VB Code:
    1. Dim myArray1(7) As Byte
    2. Dim myArray2() As Byte = New Byte(7) {}
    Since it is illegal to initialise variables when declaring in a structure, it cannot be done that way.

    Also note that because you have declared it as a public variable, anyone can simply assign a different array to that variable anyway, like this:[Highlight=VB]Dim myUUID As UUID

    myUUID.Data4 = New Byte(100) {}[Highlight=VB]If you want to make sure that the array always has 8 elements you could make it a read-only property like this:
    VB Code:
    1. Public Structure UUID
    2.     Public Data1 As Long
    3.     Public Data2 As Integer
    4.     Public Data3 As Integer
    5.     Private m_Data4() As Byte
    6.  
    7.     Public ReadOnoly Property Data4() As Byte()
    8.         Get
    9.             If Me.m_Data4 Is Nothing Then
    10.                 ReDim Me.m_Data4(7)
    11.             End If
    12.  
    13.             Return Me.m_Data4
    14.         End Get
    15.     End Property
    16. End Structure
    That way you can modify the elements of the array from outside but you cannot assign a different array to the property. Note that you can also declare public variables as ReadOnly. I assume it has the same effect, but I haven't ever tried it to find out.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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

    Re: [RESOLVED] Declaring Arrays in Structures

    If you don't care how many elements the array has then why not provide a constructor that takes the array upper bound as an argument:
    VB Code:
    1. Public Sub New(ByVal upperBound As Integer)
    2.     ReDim Me.Data4(upperBound)
    3. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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