Question Why is visual studio 8 giving declaration error

Joined
Jan 13, 2009
Messages
5
Programming Experience
Beginner
Why is visual 8.0 is giving my the following error when running the following code.

Option Explicit On
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


'Example 6 - For each w/array
Dim myArray() As String = ("Bob","Beth","Conrad","Grant")

Dim person As String
For Each person In myArray()
MessageBox.Show(person)
Next

End Sub
End Class

Error 1 ')' expected. C:\Users\bayouprophet\Documents\Visual Studio 2008\Projects\Lesson05\Lesson05\Form1.vb 8 41 Lesson05
Error 2 Name 'myArray' is not declared. C:\Users\bayouprophet\Documents\Visual Studio 2008\Projects\Lesson05\Lesson05\Form1.vb 11 28 Lesson05
 
Put braces around your elements when initializing the array.

When referencing myArray don't include the parenthesis.

VB.NET:
Option Explicit On
Public Class Form1

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

		'Example 6 - For each w/array
		Dim myArray() As String = {"Bob", "Beth", "Conrad", "Grant"}

		Dim person As String
		For Each person In myArray
			MessageBox.Show(person)
		Next

	End Sub
End Class

In the future please use [ C O D E ] tags to format code for readability.
 
Back
Top