|
-
Aug 30th, 2005, 06:10 PM
#1
[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:
Public Structure UUID
Public Data1 As Long
Public Data2 As Integer
Public Data3 As Integer
Public Data4(7) As Byte
End Structure
The function that is utilizing it refers to it here :
VB Code:
With typUUID
.Data1 = &H626FC520
.Data2 = &HA41E
.Data3 = &H11CF
.Data4(0) = &HA7
.Data4(1) = &H31
.Data4(2) = &H0
.Data4(3) = &HA0
.Data4(4) = &HC9
.Data4(5) = &H8
.Data4(6) = &H26
.Data4(7) = &H37
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??
-
Aug 30th, 2005, 06:23 PM
#2
Frenzied Member
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:
Public Structure UUID
Public Data1 As Long
Public Data2 As Integer
Public Data3 As Integer
Public Data4 As Daty
End Structure
Public Structure Daty
Friend D1 As Byte
Friend D2 As Byte
Friend D3 As Byte
Friend D4 As Byte
Friend D5 As Byte
Friend D6 As Byte
Friend D7 As Byte
Friend D8 As Byte
End Structure
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ken As New UUID
With ken
.Data1 = &H626FC520
.Data2 = &HA41E
.Data3 = &H11CF
.Data4.D1 = &HA7
.Data4.D2 = &H31
.Data4.D3 = &H0
.Data4.D4 = &HA0
.Data4.D5 = &HC9
.Data4.D6 = &H8
.Data4.D7 = &H26
.Data4.D8 = &H37
End With
End Sub
If you find my thread helpful, please remember to rate me 
-
Aug 30th, 2005, 06:27 PM
#3
Frenzied Member
Re: Declaring Arrays in Structures
Got it!
You need to redim ...of course!
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ken As New UUID
ReDim ken.Data4(7)
With ken
.Data1 = &H626FC520
.Data2 = &HA41E
.Data3 = &H11CF
.Data4(0) = &HA7
.Data4(1) = &H31
.Data4(2) = &H0
.Data4(3) = &HA0
.Data4(4) = &HC9
.Data4(5) = &H8
.Data4(6) = &H26
.Data4(7) = &H37
End With
End Sub
If you find my thread helpful, please remember to rate me 
-
Aug 30th, 2005, 06:56 PM
#4
Re: Declaring Arrays in Structures
Thx! Redim worked.... simple as that
-
Aug 30th, 2005, 07:07 PM
#5
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:
Dim myArray1(7) As Byte
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:
Public Structure UUID
Public Data1 As Long
Public Data2 As Integer
Public Data3 As Integer
Private m_Data4() As Byte
Public ReadOnoly Property Data4() As Byte()
Get
If Me.m_Data4 Is Nothing Then
ReDim Me.m_Data4(7)
End If
Return Me.m_Data4
End Get
End Property
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.
-
Aug 30th, 2005, 07:09 PM
#6
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:
Public Sub New(ByVal upperBound As Integer)
ReDim Me.Data4(upperBound)
End Sub
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
|