-
Sep 7th, 2024, 11:14 AM
#1
Thread Starter
Member
What is going on ?
Hello.
I write two simply lines
Code:
Public MyList As List(Of String)
MyList.additem("Grey Brown", "Brown Hybrid")
MyList produce error : declaration expected
Code:
Public myarray(8) As String
myarray(0)="ddd"
myarray(0) produce error : declaration expected
Code:
Dim list As New ArrayList
list.Add("One")
list.Add("Two")
list.Add("Three")
list produce error : declaration expected
Code:
Dim list As New List(Of String)({"carrot", "fox", "explorer"})
Console.WriteLine(list(0))
console produce error : declaration expected
also
Code:
the cmd MsgBox(My.Computer.FileSystem.CurrentDirectory)
Msgbox produce error : declaration expected
what is here wrong ?
Last edited by marsias; Sep 7th, 2024 at 12:29 PM.
-
Sep 7th, 2024, 05:19 PM
#2
Re: What is going on ?
Originally Posted by marsias
Hello.
I write two simply lines
Code:
Public MyList As List(Of String)
MyList.additem("Grey Brown", "Brown Hybrid")
MyList produce error : declaration expected
The code above is wrong. You must initialize the MyList variable by creating a new instance of List(Of String).
The methods AddRange and Add are the standard ways to add items to this collection.
This is what your code should look like:
Code:
' Should always be Private, not Public
Private MyList As List(Of String) = New List(Of String)()
' Add items in method of your choosing
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MyList.AddRange({"Grey Brown", "Brown Hybrid"})
End Sub
Or this:
Code:
Private MyList As List(Of String) = New List(Of String)()
' Add items in method of your choosing
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MyList.Add("Grey Brown")
MyList.Add("Brown Hybrid")
End Sub
Or shorten it all to a single line of code within a method:
Code:
' Add items in method of your choosing
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim MyList As New List(Of String) From {"Grey Brown", "Brown Hybrid"}
End Sub
Last edited by Peter Porter; Sep 8th, 2024 at 07:11 AM.
-
Sep 8th, 2024, 12:43 AM
#3
Thread Starter
Member
Re: What is going on ?
hello.
the "correct code" produces also the same errors !!!
Code:
Public MyList As List(Of String) = New List(Of String)()
MyList.Add("Grey Brown")
MyList.Add("Brown Hybrid")
Only the short code produces no error
Code:
Dim MyList As New List(Of String) From {"Grey Brown", "Brown Hybrid"}
PS: something was with the Module, i have remove the Module yet, the errors persist.
The app works as is , but i cant insert any code which initialize anything.
Last edited by marsias; Sep 8th, 2024 at 01:08 AM.
-
Sep 8th, 2024, 03:44 AM
#4
Re: What is going on ?
Where are you putting your code? Your first examples would need to be in a sub or function.
-
Sep 8th, 2024, 04:47 AM
#5
Thread Starter
Member
Re: What is going on ?
you are right my mistakes....was outside a sub
thank you
-
Sep 8th, 2024, 05:38 AM
#6
Re: What is going on ?
Originally Posted by marsias
Code:
Public MyList As List(Of String) = New List(Of String)()
MyList.Add("Grey Brown")
MyList.Add("Brown Hybrid")
That code makes no sense as it is. You can only specify an access level, e.g. Public, on a variable if it is declared at the class level but the next two lines can't be written at the class level. That's your problem. Try learning the basics. Only declarations are allowed at the class level. Everything else must be done in a method. If that code is in a form, you might add the items to the List in the Load event handler. Alternatively, you can populate the List where you declare the variable:
Code:
Public MyList As New List(Of String) From {"Grey Brown", "Brown Hybrid"}
Of course, you almost certainly shouldn't be declaring a field Public in the first place. If it's a field then it should be Private and if it's Public then it should be a property. Maybe it should only be a local variable in a method, but we can't really tell without context.
-
Sep 8th, 2024, 06:18 AM
#7
Re: What is going on ?
Originally Posted by jmcilhinney
That code makes no sense as it is. You can only specify an access level, e.g. Public, on a variable if it is declared at the class level but the next two lines can't be written at the class level. That's your problem. Try learning the basics. Only declarations are allowed at the class level. Everything else must be done in a method.
I figured he pulled the two lines populating the list from a method and placed it under the class level line just for show here, and I should've remembered that it should be Private. Others might see this as proper coding. My mistake. I corrected my reply.
This is what the code should look like (the method for adding items doesn't have to be Form1_Load):
Code:
Public Class Form1_Load
' Should always be Private, not Public
Private MyList As List(Of String) = New List(Of String)()
' Add items in method of your choosing
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MyList.Add("Grey Brown")
MyList.Add("Brown Hybrid")
End Sub
End Class
Last edited by Peter Porter; Sep 8th, 2024 at 10:05 AM.
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
|