how to check that a list contains another list item in VB.Net - vb.net

I have two lists like this.
Dim List1 As IList(Of String) = New List(Of String)
Dim ListMatch As IList(Of String) = New List(Of String)
I need to figure out if List1 contains all items of ListMatch. How can i do this is VB.Net?

You can use Not SmallCollection.Except(LargeCollection).Any:
Dim containsAll = Not ListMatch.Except(List1).Any()
Documentation of Enumerable.Except:
This method returns those elements in first that do not appear in
second. It does not also return those elements in second that do not
appear in first.
Since Except is a set method, it doesn't take duplicates into account. So if you also want to know if List1 contains the same count of items of ListMatch you could use(less efficient):
Dim list1Lookup = List1.ToLookup(Function(str) str)
Dim listMatchLookup = ListMatch.ToLookup(Function(str) str)
Dim containsSameCount = listMatchLookup.
All(Function(x) list1Lookup(x.Key).Count() = x.Count())

Related

Is there a method to add to a list (of list(of String)) byVal?

I'd like to repeatedly add lists of strings to a bigger list collection, but it seems that the list.add function is adding the items byref. How can I change the below example code to pass the elements byval so that they're not cleared when I re-use variables:
Dim inner As New List(Of String)
Dim outer As New List(Of List(Of String))
Dim eleCount As Integer = 0
lbltop:
inner.Add("a")
inner.Add("b")
inner.Add("c")
outer.Add(inner)
Debug.Write(outer(0)(0).ToString())
inner.Clear()
Debug.Write(outer(0)(0).ToString())
eleCount += 1
If eleCount < 2 Then
GoTo lbltop
End If
this writes a then there's an out of range exception for the next debug.write statement.
I'd like it to write aa then loop to add another inner element.
List<T> is a reference type. Only way to not effect it when calling Clear() is to add a clone of it to outer instead. You can do so by using .ToList()
outer.Add(inner.ToList())
But I actually think the most clear way is to instead assign inner a new List instead of calling Clear, than you dont have to do above.
inner = new List(Of String)

vb.net - How to concatenate a variable(trimmed) to another variable to complete its variable name

Needed code is something like this:
Dim myArray(0) As String
Dim ay As String = "ay"
myArr & ay(0) = "asd"
I've tried but did not worked
Dim classlist1(0) As String
Dim classlist2(0) As String
Dim classlist3(0) As String
Dim classlist4(0) As String
Dim count As Integer = 0
For _year As Integer = 1 To 4
("classlist" & _year)(count) = "hi"
count += 1
Next
Any time you see something like this:
Dim classlist1(0) As String
Dim classlist2(0) As String
Dim classlist3(0) As String
' etc.
It's an indication that you're using the wrong data structure. Instead of trying to dynamically build variable names (which isn't really possible in a static language, at least not without some really ugly reflection code with a high potential for runtime errors), just use a collection.
For example, if you want a collection of strings:
Dim classList As New List(Of String)()
And if you want a collection of collections of strings:
Dim classLists As New List(Of List(Of String))()
Then you can reference the nested lists within the parent list. So to add your first "year" of classes:
classLists.Add(new List(Of String))
And add a class to that year:
classLists(0).Add("some value")
As you can see, it starts to get a little difficult to keep track of the data structures. This is where creating custom types and structures becomes very useful. For example, rather than representing a "year" as a list of strings, create an actual Year class. That class can internally hold a list of strings, and other logic/data.
Try Dictionary<TKey, TValue> Class From MSDN.
Dim classLists As New Dictionary(Of String, String)()
'Add items with keys
For _year As Integer = 1 To 4
classLists.Add(String.Format("classlist{0}",_year), "hi")
Next
And you can get value by key later
Dim key As String = "classlist2"
Dim value As String = classLists(key)

List of lists in vb.net

I am trying to create lists, that are then inserted into another list.
For some reason the latter list gets overwritten each time I try to add new list item to it.
For the code below, first I want to add items to Temp list and after certain conditions have been met, add the Temp list as an item to the Comp list. After that, the cycle repeats, new and different Temp list should be created and the added to the Comp list as next item. So each item in Comp list should be different.
But in the end I get a Comp list that is filled with Temp lists that are all identical to the last Temp list added.
What am I doing wrong?
Function UniqueValueList2(ByVal InputObject As List(Of Object)) As List(Of List(Of Object))
Dim vc As Integer = InputObject.Count
Dim i As Integer = 1
Dim Temp As New List(Of Object)
Dim Comp As New List(Of List(Of Object))
Dim CurrentObj As String
Dim PrevObj As String
Temp.Add(InputObject(0))
Do While i < vc
CurrentObj = InputObject(i).fieldName
PrevObj = InputObject(i-1).fieldName
If CurrentObj = PrevObj Then
Temp.Add(InputObject(i))
Else
Comp.Add(Temp)
Temp.Clear()
Temp.Add(InputObject(i))
End If
i = i + 1
Loop
Comp.Add(Temp)
UniqueValueList2 = Comp
End Function
Temp is holding the same reference. so making changes on it will change it.And you add and modify the same List
Comp.Add(Temp) 'the same Temp List
Temp.Clear() 'You will clear the same List
Temp.Add(InputObject(i))
So How you should do :
Comp.Add(Temp) 'we add old List
Temp=New List(Of Object) 'Temp now holds reference to new List
Temp.Add(InputObject(i))
This will work:
Comp.Add(Temp.ToList())
Temp.Clear()
Temp.Add(InputObject(i))

What collection method should I use?

I have a text file and it looks like this
defabot:3215
defatank:2985
ludditus:29
compensate:355
moobot:805
maxi292003:63
doctorbarzhal:19
plaanz:4
manguuu:51
atntpc:19
immortalsofthemist:18
I'm loading this list when my application opens, splitting the name from number and populating a listview control with it. I want to maintain the list in memory for my entire project to use and manipulate.
I need the numbers associated with the name so when I search for the name I know how many points they have. What is the most efficient/easiest way to accomplish this?
Thank you.
Use Dictionary(Of String, Integer). Here is an example:
Dim dict As New Dictionary(Of String, Integer)
For Each s As String In IO.File.ReadAllLines("filename")
Dim a() As String = s.Split(":"c)
Dim userName As String = a(0)
Dim userPoints As Integer = Convert.ToInt32(a(1))
dict.Add(userName, userPoints)
Next
The most efficient way is to use a Dictionary(Of String, Int32):
Dim dict As Dictionary(Of String, Int32) = IO.File.ReadLines(path).
Select(Function(line) line.Split(":"c)).
ToDictionary(Function(split) split(0), Function(split) Int32.Parse(split(1)))
You get the value by it's key:
Dim ludditus As Int32 = dict("ludditus")

vb.net - multi-dimension array list

I've managed to make some single dimension array lists but I can't figure out a multi dimension arraylist.
Here's what I'm trying to do:
I have a database (mdb) with 5 columns that I want each row to be in an array list.
In PHP what I'd typically do is:
$array[$field1] = array($field2,$field3,$field4,$field5);
How I do the same in vb.net so anytime I need to fetch an item for a specific for the row1 I could call it?
For a single dimension I could do the following, but I can't figure out how to add more fields to a single array row:
Dim tmpArrayX As New ArrayList
tmpArrayX.Add(field(0))
tmpArrayX.Add(field(1))
etc...
If you want to use ArrayList, just make it's items contain other ArrayLists.
Or you could use normal arrays as:
Dim multiArray(2, 2) As String
multiArray(0, 0) = "item1InRow1"
multiArray(0, 1) = "item2InRow1"
multiArray(1, 0) = "item1InRow2"
multiArray(1, 1) = "item2InRow2"
Though my personal preference would be to use List as:
Dim multiList As New List(Of List(Of String))
multiList.Add(New List(Of String))
multiList.Add(New List(Of String))
multiList(0).Add("item1InRow1")
multiList(0).Add("item2InRow1")
multiList(1).Add("item1InRow2")
multiList(1).Add("item2InRow2")
Edit: How to find row:
Dim listIWant As List(Of String) = Nothing
For Each l As List(Of String) In multiList
If l.Contains("item1InRow2") Then
listIWant = l
Exit For
End If
Next
' This allows adding rows on the fly....Tested and it works!
Dim multiList As New List(Of List(Of String))
Dim ListRow As Integer = 0
For each record in some_source
dim Country as string = record.country 'from some source
dim Date as Date = record.Date 'from some source
dim Venue as string = record.Venue 'from some source
dim Attendance as string = record.Attendance 'from some source
multiList.Add(New List(Of String))
multiList(ListRow).Add(Country)
multiList(ListRow).Add(Date)
multiList(ListRow).Add(Venue)
multiList(ListRow).Add(Rating)
multiList(ListRow).Add(Attendance)
ListRow = ListRow + 1
next