Array of Undefined size - Declaration in Structure causes pass over/hang?
Hello again!
I'm having a problem whilst attempting to create a small database of sorts.
When trying:
Code:
Public Class Form1
Structure Items
Dim Food As String
Dim Extras() As String
Dim Price As Double
End Structure
Dim Food(6) As Items
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Food(0).Food = "Burger"
Food(1).Food = "Chkn Burger"
Food(2).Food = "Sausage"
Food(3).Food = "Nuggets!!!!"
Food(4).Food = "CornDog"
Food(5).Food = "Fries"
Food(6).Food = "Onion Rings"
' ''Burger
Label1.Text = "One"
Food(0).Extras(0) = "Cheese"
Food(0).Extras(1) = "Bacon"
Food(0).Extras(2) = "Extra Onions"
''Chkn Burg.
Label1.Text = "Two"
Food(1).Extras(0) = "Cheese"
Food(1).Extras(1) = "Xtra Sauce"
'' Sausage
Food(2).Extras(0) = "Cheese"
Food(2).Extras(1) = "Chilli"
'Nuggets
Food(3).Extras(0) = "Plum"
Food(3).Extras(1) = "Sweet & Sour"
Food(0).Price = 1.25
Food(1).Price = 1.75
Food(2).Price = 2.0
Food(3).Price = 2.0
For X = 0 To Food.Length
cbItemChoose.Items.Add(Food(X).Food)
Next
End Sub
It runs the program without error, but the "cbItemChoose" Dropdown does't get populated. I put the lines to change the "Label1" as a test, at which it gets to "One" but never "two".
If I comment out every line that has the Food(x).Extras(y) in it, the program populates the dropdown, but runs into an error as expected when trying to use the extras.
I'm not sure if there's another way to declare this, or if it's just another error on my fault of HOW I'm declaring this.
You'll notice, should you choose to look at the attachment, that I have a lot of variables at the top. The point of this structure is to make rid of all this messy declaration, as I am striving for efficiency.
Re: Array of Undefined size - Declaration in Structure causes pass over/hang?
You can't create an array without specifying the size. In your Items structure you haven't specified the size of the Extras array, therefore there is no array. There's a field that can refer to an array, but there's no array. If you want an array you have to create one, in which case you have to specify the size of it, e.g.
Re: Array of Undefined size - Declaration in Structure causes pass over/hang?
One other point: What appears to be happening is that you are getting an exception in the Form Load event. There is an interesting (to put it politely) feature in some hardware/OS combinations where an exception in the Load event will not be seen. All that happens in this case is that execution of the Load event code simply ends. That appears to be a pretty fair description of what you are seeing.
If this is being written for a class, have you covered structured exception handling? In this case, you will see the exception only if you wrap the code in a Try...Catch clause, and show the exception in a messagebox from the Catch clause. Also, as far as I know, this ONLY impacts Form Load, and it is not likely to be fixed anytime soon, since MS doesn't really feel that it is a bug.
Re: Array of Undefined size - Declaration in Structure causes pass over/hang?
Originally Posted by Shaggy Hiker
One other point: What appears to be happening is that you are getting an exception in the Form Load event. There is an interesting (to put it politely) feature in some hardware/OS combinations where an exception in the Load event will not be seen. All that happens in this case is that execution of the Load event code simply ends. That appears to be a pretty fair description of what you are seeing.
If this is being written for a class, have you covered structured exception handling? In this case, you will see the exception only if you wrap the code in a Try...Catch clause, and show the exception in a messagebox from the Catch clause. Also, as far as I know, this ONLY impacts Form Load, and it is not likely to be fixed anytime soon, since MS doesn't really feel that it is a bug.
If I recall correctly, this is an issue on 64-bit Windows and it results from a difference of opinion between the .NET team and the Windows team. The .NET folks can't "fix" the problem without cooperation from the Windows folks, who aren't prepared to play ball. Presumably the change would have ramifications other than making Windows Forms development a better experience.