Why does VB.NET behave like this with lists? - vb.net

This is a question concerning a list of lists.
Dim smallList As New List(Of Integer)
Dim largeList As New List(Of List(Of Integer))
smallList.Add(3)
largeList.Add(smallList)
smallList.Clear()
smallList.Add(4)
largeList.Add(smallList)
In this code, I would expect largeList to add the list (3) to itself, and then to add the list (4) to itself. But instead of storing the data inside smallList, it seems to store a reference smallList instead, so ends up containing ((4), (4)), which is not what I want.
Why does it do this, and how can I get around it? Thanks.

When you have a list of reference types, you actually have a list of references. Adding something to the list doesn't mean that the data is copied, it's just the reference that is added to the list.
To add separate objects to the list, you have to create a new object for each item, and as lists are reference types themselves, that goes for lists too.
Dim smallList As List(Of Integer) ' just a reference at this time
Dim largeList As New List(Of List(Of Integer))
smallList = New List(Of Integer)() ' The first list
smallList.Add(3)
largeList.Add(smallList)
smallList = New List(Of Integer)() ' Here's another list
smallList.Add(4)
largeList.Add(smallList)

Related

Value of type List(Of Long) cannot be converted to List(Of ListItem)

I am filling data from removeActiveService (list of listItem) into a new list listRemove. However listRemove is turning into a list of Long.
Dim listRemove = removeActiveService.Select(Function(item) item.Text.Replace(serviceRemove, "") And item.Text.Split("-"c)(0).Trim And item.Value.Split("-"c)(1).Trim).ToList()
If I change it to Dim listRemove As List(Of ListItem), it results in the error
Value of type List(Of Long) cannot be converted to List(Of ListItem)
I need to perform replace and split on the text and value (see my code). What is the correct syntax here so that it can be a List(Of ListItem)?
EDIT
Starting with checkboxlist items in removeActiveService
Copy that into a brand new list of items called listRemove.
I need to perform these on the TEXT of the list items in listRemove
item.Text.Replace(serviceRemove, "")
item.Text.Split("-"c)(0).Trim
And I need to perform this on the VALUE of the list items in listRemove
item.Value.Split("-"c)(1).Trim
If you expect to output a List(Of ListItem) then you need to create ListItem objects somewhere, which you're not doing now. You're performing operations on the input data but you're not doing anything useful with the results. And doesn't do anything that could be considered useful there. If you expect to output a list of ListItem objects containing the results of those operations then you actually have to create ListItem objects containing the result of those operations. As far as I can tell, this would be the way to go:
Dim listRemove = removeActiveService.Select(Function(item) New ListItem With
{
.Text = item.Text.Replace(serviceRemove, "").Split("-"c)(0).Trim(),
.Value = item.Value.Split("-"c)(1).Trim()
}).
ToList()

Load list from another list in one line

I cannot figure out how to populate one list with another list in one line.
My function GetList is retreiving List(Of Decimal).
What I have tried so far is:
Dim somelist As New List(Of Decimal) From {MyObject.GetListOfDecimal()}
To assign the return value of GetListOfDecimal() to a new List(Of Decimal) this is the code you require:
Dim somelist As List(Of Decimal) = MyObject.GetListOfDecimal()
You don't require the New keyword as you are directly assigning the list to it.

Vb.net access an item in a list inside a dictionary

I created a Dictionary in VB.net to contained a list
Dim dic As New Dictionary(Of String, List(Of Double))
Then on a loop I add a Key and Items to the list.
dic.Add("Key1", New List(Of Double))
do some stuff and add the items to the list of the key
Dic("Key1").Add(1.1078)
Dic("Key1").Add(12.232)
Dic("Key1").Add(33.365)
etc
How Do I access the value of the list using a Key
Console.writeline(Dic.Item("Key1")(1))
I was trying to print index 1 of the list associated with Key1
but I get a crash
Thanks
Kiko
You have found the answer meanwhile. I would just like to add that a safer method would be to use TryGetValue:
Dim list As List(Of Double)
If dic.TryGetValue("Key1", list) Then
Console.WriteLine(list(0))
Else
Console.WriteLine("Key1 not found!")
End If
It is so simple.
dic("key1").item(1)

initialize, set, and Return Structure inline?

In vb.net, is it possible to initialize, set, and Return a Structure inline?
If I have this Structure
Structure CopyAndPasteUniquesAndExistenceStructure
Dim duplicatePoints As List(Of Array)
Dim inexistentPoints As List(Of Array)
Dim insertedRows As List(Of Object)
End Structure
How can I do this pseudo?
Return New CopyAndPasteUniquesAndExistenceStructure...
Return New CopyAndPasteUniquesAndExistenceStructure With {.duplicatePoints = New List(Of Array), .inexistentPoints = New List(Of Array), .insertedRows = New List(Of Object)}
Or you can add a specialized constructor. This is useful if you want to validate the input, or if you want to make the lists read-only (which usually makes a lot of sense for lists).

VB: List(Of List(Of String)) keeps changing the content of the outer list when I change the inner one?

I'm writing a program in VB, and I need to make a list of lists (I've already figured out how to do that one). The problem is, the outer list is going to need a different number of elements depending on other variables elsewhere in the program.
I've looped this code:
Dim rep As Long = 1023
Dim items As List(Of String)
items.Add("First Entry")
items.Add("Second Entry")
items.Add("Third Entry")
items.Add("Fourth Entry")
'(sake of argument, these are the variables
'that will be changing vastly earlier
'in the program, I put them in this way to simplify
'this part of my code and still have it work)
Dim myList As New List(Of List(Of String))
Dim tempList As New List(Of String)
For index = 1 To Len(rep.ToString)
tempList.Add(items(CInt(Mid(rep.ToString, index, 1))))
Next
myList.Add(tempList)
tempList.Clear()
My issue is with that last part; every time I add the tempList to myList, it's fine, but when I clear tempList, it also clears the version of tempList in myList.
myList will have a count of 1, but the list inside it has a count of 0 as soon as I clear tempList. And I have to clear tempList because I'm looping this section of code over and over, a variable number of times.
Is there a way around this? Am I being a horrible noob?
You're using the same tempList each time, instead of making a new one.
You likely need to do:
myList.Add(tempList)
tempList = new List(Of String) ' Create a new List(Of T), don't reuse...