Results 1 to 10 of 10

Thread: [RESOLVED] Why can't I add to a list of string ?

  1. #1

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,426

    Resolved [RESOLVED] Why can't I add to a list of string ?

    Hi,

    I have a Global List(Of String)
    Code:
     
    Public Class Form1
    
        ReadOnly Lst() As String = {"dummy", "Places", "Fiction", "Words", "People"}
        Dim Lyst(5) As List(Of String)
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Part of a subroutine contains the code...
    vb.net Code:
    1. Dim inputs(), Lizt, txt As String
    2.  
    3.         For i = 1 To 4
    4.             Lizt = My.Resources.ResourceManager.GetString(Lst(i))
    5.             inputs = Lizt.Split(vbCrLf)
    6.             For j = 0 To inputs.Length - 1
    7.                 txt = inputs(j).Trim
    8.                 Lyst(i).Add(txt)
    9.             Next
    10.         Next
    The code runs as I expect up to Line 8 when I get the error "Object reference not set to an instance of an object."

    Looking at the Watch Window, There are 6 Lyst() lists, but they all say 'Nothing'.
    Why is that? Why can't I add data to these lists?


    Poppa
    Along with the sunshine there has to be a little rain sometime.

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

    Re: Why can't I add to a list of string ?

    This line:
    vb.net Code:
    1. Dim Lyst(5) As List(Of String)
    creates an array with six elements and, as with ALL arrays, each of those elements is Nothing until you actually assign something to it. Are you doing that anywhere? Nowhere that you showed us so of course here:
    vb.net Code:
    1. Lyst(i).Add(txt)
    indexing the array returns Nothing.

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

    Re: Why can't I add to a list of string ?

    Quote Originally Posted by Poppa Mintin View Post
    Hi,

    I have a Global List(Of String)
    Code:
     
    Public Class Form1
    
        ReadOnly Lst() As String = {"dummy", "Places", "Fiction", "Words", "People"}
        Dim Lyst(5) As List(Of String)
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Part of a subroutine contains the code...
    vb.net Code:
    1. Dim inputs(), Lizt, txt As String
    2.  
    3.         For i = 1 To 4
    4.             Lizt = My.Resources.ResourceManager.GetString(Lst(i))
    5.             inputs = Lizt.Split(vbCrLf)
    6.             For j = 0 To inputs.Length - 1
    7.                 txt = inputs(j).Trim
    8.                 Lyst(i).Add(txt)
    9.             Next
    10.         Next
    The code runs as I expect up to Line 8 when I get the error "Object reference not set to an instance of an object."

    Looking at the Watch Window, There are 6 Lyst() lists, but they all say 'Nothing'.
    Why is that? Why can't I add data to these lists?


    Poppa
    Could you not come up with more helpful variable names? Just reading through that it is confusing to figure out exactly what Lizt, Lyst,and Lst are!

    Also you don't have a global List(Of String) either, you have a class level variable - not a global variable.

  4. #4

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,426

    Re: Why can't I add to a list of string ?

    Thanks john,

    An example of how to do that would help.


    Poppa
    Along with the sunshine there has to be a little rain sometime.

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

    Re: Why can't I add to a list of string ?

    Quote Originally Posted by Poppa Mintin View Post
    Thanks john,

    An example of how to do that would help.


    Poppa
    Code:
    Lyst(0) = New List(Of String)

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Why can't I add to a list of string ?

    Quote Originally Posted by Poppa Mintin View Post
    Thanks john,

    An example of how to do that would help.


    Poppa
    Code:
        Private Lyst(5) As List(Of String)
        Private Sub Form1_Load(sender As Object,
                                e As EventArgs) Handles MyBase.Load
            For x As Integer = 0 To Lyst.Length - 1
                Lyst(x) = New List(Of String)
            Next
        End Sub
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Why can't I add to a list of string ?

    You can do it during initialization:
    Code:
    Private listsCollection() As List(Of String) = Enumerable.Range(1, 6).Select(Function(index) New List(Of String)()).ToArray()
    Fiddle: https://dotnetfiddle.net/scAR4W

    Explination:
    1. Declare an array of List(Of String)
      1. Create a range of numbers from 1 to 6
      2. Return a new List(Of String) for each number
      3. Call ToArray() to convert the IEnumerable to an array
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Why can't I add to a list of string ?

    Quote Originally Posted by dday9 View Post
    You can do it during initialization:
    Code:
    Private listsCollection() As List(Of String) = Enumerable.Range(1, 6).Select(Function(index) New List(Of String)()).ToArray()
    Fiddle: https://dotnetfiddle.net/scAR4W

    Explination:
    1. Declare an array of List(Of String)
      1. Create a range of numbers from 1 to 6
      2. Return a new List(Of String) for each number
      3. Call ToArray() to convert the IEnumerable to an array
    Nice!
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  9. #9

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,426

    Re: Why can't I add to a list of string ?

    Thanks guys,

    I used dbasnett's code, it all works well and very much faster than the way I was trying to get around it. Thanks dbasnett.


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  10. #10
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Why can't I add to a list of string ?

    Quote Originally Posted by Poppa Mintin View Post
    Thanks guys,

    I used dbasnett's code, it all works well and very much faster than the way I was trying to get around it. Thanks dbasnett.


    Poppa
    You are welcome. dday9's code would also work.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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