-
Sep 30th, 2022, 07:42 AM
#1
Thread Starter
PowerPoster
[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:
Dim inputs(), Lizt, txt As String
For i = 1 To 4
Lizt = My.Resources.ResourceManager.GetString(Lst(i))
inputs = Lizt.Split(vbCrLf)
For j = 0 To inputs.Length - 1
txt = inputs(j).Trim
Lyst(i).Add(txt)
Next
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.
-
Sep 30th, 2022, 08:11 AM
#2
Re: Why can't I add to a list of string ?
This line:
vb.net Code:
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:
indexing the array returns Nothing.
-
Sep 30th, 2022, 08:37 AM
#3
Re: Why can't I add to a list of string ?
Originally Posted by Poppa Mintin
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:
Dim inputs(), Lizt, txt As String
For i = 1 To 4
Lizt = My.Resources.ResourceManager.GetString(Lst(i))
inputs = Lizt.Split(vbCrLf)
For j = 0 To inputs.Length - 1
txt = inputs(j).Trim
Lyst(i).Add(txt)
Next
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.
-
Sep 30th, 2022, 08:45 AM
#4
Thread Starter
PowerPoster
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.
-
Sep 30th, 2022, 08:59 AM
#5
Re: Why can't I add to a list of string ?
Originally Posted by Poppa Mintin
Thanks john,
An example of how to do that would help.
Poppa
Code:
Lyst(0) = New List(Of String)
-
Sep 30th, 2022, 09:46 AM
#6
Re: Why can't I add to a list of string ?
Originally Posted by Poppa Mintin
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
-
Sep 30th, 2022, 10:56 AM
#7
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:
- Declare an array of List(Of String)
- Create a range of numbers from 1 to 6
- Return a new List(Of String) for each number
- Call ToArray() to convert the IEnumerable to an array
-
Sep 30th, 2022, 11:36 AM
#8
Re: Why can't I add to a list of string ?
Originally Posted by dday9
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:
- Declare an array of List(Of String)
- Create a range of numbers from 1 to 6
- Return a new List(Of String) for each number
- Call ToArray() to convert the IEnumerable to an array
Nice!
-
Sep 30th, 2022, 11:40 AM
#9
Thread Starter
PowerPoster
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.
-
Sep 30th, 2022, 11:50 AM
#10
Re: Why can't I add to a list of string ?
Originally Posted by Poppa Mintin
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.
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
|