vb.net - multi-dimension array list - vb.net

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

Related

how to check that a list contains another list item in 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())

Add Items to datatale per column instead of per row

Half a day of searching made at least my data readable in a datagrid in a wpf app.
My problem is (or what in think is my problem) would be the adding of data in the columns I created.
My source of data is from a dictionary of string, list(of string) with my dictionary key as the header and the List as it's items, also the dictionary and it's content has no fixed length.
And to display my data I used the combination of DataTable and DataGrid
Here's my snippet
Dim files As New Dictionary(Of String, List(Of String))
Dim dt As New DataTable
For Each item As KeyValuePair(Of String, List(Of String)) In files
dt.Columns.Add(item.Key)
For Each file As String In item.Value
Dim dr As DataRow = dt.NewRow
dr(dt.Columns(item.Key).Ordinal) = file
dt.Rows.Add(dr)
Next
Next
DGrid.ItemsSource = dt.AsDataView
It works(somewhat), but as you all have guessed all the succeeding displayed column items after the last one starts where it ended last(looks like a stair).
so the question is do I have to change the way I store my data(dictionary) or I am missing something and this can work as there is a way to add data per column instead of rows?
UPDATE
It's not pretty and I know there is a better solution out there.
What I did first was to just render/instantiate the table and it's content
Dim dt As New DataTable
For Each item As KeyValuePair(Of String, List(Of String)) In files
dt.Columns.Add(item.Key)
For Each file As String In item.Value
Dim dr As DataRow = dt.NewRow
dr(item.Key) = Nothing
dt.Rows.Add(dr)
Next
Next
Then edit it's content:
For Each item As KeyValuePair(Of String, List(Of String)) In files
For file_index As Integer = 0 To item.Value.Count - 1
dt.Rows(file_index)(item.Key) = item.Value(file_index)
Next
Next
This is a bad solution but it works. Is there a better way?
you need to reuse existing rows for this. And add new one only when you have more items then rows.
Dim Files As New Dictionary(Of String, List(Of String))
Dim T As New DataTable, R As DataRow
For Each Pair As KeyValuePair(Of String, List(Of String)) In Files
T.Columns.Add(Pair.Key)
For i As Integer = 0 To Pair.Value.Count - 1
If i < T.Rows.Count Then
R = T.Rows(i)
Else
R = T.NewRow()
T.Rows.Add(R)
End If
R(Pair.Key) = Pair.Value(i)
Next
Next

Datagridview to report viewer using parameter VB.net

I try to copy all columns in my datagridview (DGView7) to my report viewer.
I used this code, but it didn't work
Dim myAL As New ArrayList()
Dim row As List(Of String)
For i As Integer = 0 To Basicfor.DGView7.RowCount() - 1
row = New List(Of String)
For j As Integer = 0 To Basicfor.DGView7.ColumnCount() - 1
row.Add(Basicfor.DGView7.Rows(i).Cells(j).ToString)
Next j
myAL.Add(row)
Next i
Dim Params(0) As ReportParameter
Params(0) = New ReportParameter("Ref", CType(myAL.ToArray(GetType(String)), String()))
Me.ReportViewer1.RefreshReport()
It shows an error. I tried to change to this code string() -> Single(), but single is not defined
Params(0) = New ReportParameter("Ref", CType(myAL.ToArray(GetType(String)), Single()))
Can anyone resolve this? Thanks in advance
You're calling
Params(0) = New ReportParameter("Ref", CType(myAL.ToArray(GetType(String)), String()))
And wanting to pass in an array of strings(). That's my assumption at least
However you're passing an array of list of strings.
Try
Dim myStrings = myAL.Cast(Of List(Of String)).SelectMany(Function(r) r).ToArray()

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