Load list from another list in one line - vb.net

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.

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()

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)

Defining the type of a List in vb.net at runtime

I'm creating a class at run time using typebuilder and after I create this class I want to define its type for a list like
dim fooList as new List(of DynamicClassName)
Since this doesn't exist at compile time of course it throws an error. When I generate this type I return the type so I can't do something like
dim newType = createNewType(foobar)
dim fooList as new List(of getType(newType))
How do I assign the type of a List at runtime?
You can create a List(Of T), but AFAIK you won't be able to cast it to a typed object. I've used the String type in the following example.
Dim list As Object = Activator.CreateInstance(GetType(List(Of )).MakeGenericType(New Type() {GetType(String)}))
Debug.WriteLine((TypeOf list Is List(Of String)).ToString())
Output
True
So in your case it would look like this:
Dim newType = createNewType(foobar)
'Creates a List(Of foobar):
Dim list As IList = Ctype(Activator.CreateInstance(GetType(List(Of )).MakeGenericType(New Type() {newType})), IList)
'Creates a BindingList(Of foobar):
Dim bindingList As IBindingList = Ctype(Activator.CreateInstance(GetType(BindingList(Of )).MakeGenericType(New Type() {newType})), IBindingList)
This does not answer your question, but may solve your problem.
Another option would be to use an ArrayList (for which you don't have to assign a type). You can see the details here: http://msdn.microsoft.com/en-us/library/system.collections.arraylist(v=vs.110).aspx.
Here is a basic example:
Dim anyArrayList as new ArrayList()
anyArrayList.add("Hello")
anyArrayList.add("Testing")

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...

Why does VB.NET behave like this with lists?

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)