Results 1 to 7 of 7

Thread: What is going on ?

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2023
    Posts
    54

    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.

  2. #2
    Fanatic Member Peter Porter's Avatar
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    543

    Re: What is going on ?

    Quote Originally Posted by marsias View Post
    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.

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2023
    Posts
    54

    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.

  4. #4
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,709

    Re: What is going on ?

    Where are you putting your code? Your first examples would need to be in a sub or function.

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2023
    Posts
    54

    Re: What is going on ?

    you are right my mistakes....was outside a sub

    thank you

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

    Re: What is going on ?

    Quote Originally Posted by marsias View Post
    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.
    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

  7. #7
    Fanatic Member Peter Porter's Avatar
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    543

    Re: What is going on ?

    Quote Originally Posted by jmcilhinney View Post
    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
  •  



Click Here to Expand Forum to Full Width