how to create a list within a list - vb.net

Hello this is my first time posting so bear with me if I'm not so on point.
Here is my question:
I'm trying to create a list within a list in asp.net,
something like
callList(a).part(b).Number
callList(a).part(b).Desc
I have service calls and each of these calls can have multiple parts listed in them.
Right now the class structure is something like
public class calls
somevars.....
public class part
public Number as integer
public desc as String
end class
end class
To create the call themselves I have
callList As New List(Of calls)
callList.add(calls)
How would I add multiple parts for each call?

Maybe this is what you are looking for. I used different names, but you'll figure it out :)
Public Class clsContent
Public Name As String
Public listOfCounteries As List(Of clsCountry)
End Class
Public Class clsCountry
Public countryName As String
End Class
Class Program
Private Shared Sub Main(args As String())
Dim _countryEgypt As New clsCountry()
_countryEgypt.countryName = "Egypt"
Dim _countrySudan As New clsCountry()
_countrySudan.countryName = "Sudan"
Dim _cont As New clsContent()
_cont.Name = "Africa"
_cont.listOfCounteries = New List(Of clsCountry)()
_cont.listOfCounteries.Add(_countryEgypt)
_cont.listOfCounteries.Add(_countrySudan)
Dim _listOfContenents As New List(Of clsContent)()
_listOfContenents.Add(_cont)
Console.WriteLine((("Contenent: " + _listOfContenents(0).Name & " Country 1: ") + _listOfContenents(0).listOfCounteries(0).countryName & " Country 2: ") + _listOfContenents(0).listOfCounteries(1).countryName)
Console.Read()
End Sub
End Class

I think what you're looking for is described here under nested lists (slight syntax changes for ASP.net vs VB.net)
add a list into another list in vb.net
EDIT:
If you have trouble with the link, what you'll find there is:
Hope this helps
Update Reading them is like accessing any other list. To get the
first field in the first record
return records(0)(0)
second field in first record return records(0)(1)
etc . . .
Dim listRecord As New List(Of String)
listRecord.Add(txtRating.Text)
listRecord.Add(txtAge.Text)
listRace.Add(listRecord)
Dim records as new List(of List(of String))
records.Add(listRecord)
He creates a
List(Of String)
and then adds it to a
List(Of List(Of String))

Related

SortedSet in Class does not expose correctly

I have a weird problem that I can't seem to figure out. Even weirder is that I'm fairly sure it has worked in the past, but not anymore.
I have a class where I define a variable as SortedSet. In a function, I can reference the variable, but its SortedSet attributes are not exposed. If I use them anyway, some of them work, others don't. If I create that variable inside my function, all works as expected.
This is the code:
Public Class MyTest
Public MySortedSet = New SortedSet(Of String)()
Public Sub New()
Dim MySortedSet2 = New SortedSet(Of String)()
'Constructor. To use this, add Dim MyTest As MyTest to the Form1_load sub.
Me.MySortedSet.add("Test")
For Each Item In Me.MySortedSet
MsgBox(Item) 'This does print Test
Next Item
Me.MySortedSet.add '.add not exposed
MySortedSet2.add '.add is exposed
End Sub
End Class
See the screenshot below. The first example only has 4 items, where the 2nd example has a full list of parameters. I need to fix this using the first example, so the ElementAt one works. It works in the second example, but not in the first. It gives the error that ElementAt is not part of this object.
How can I get the full list of parameters for me.MySortedSet.??????
You should declare MySortedSet as a Property:
Public Class MyTest
Public Property MySortedSet As New SortedSet(Of String)()
Public Sub New()
Dim MySortedSet2 As New SortedSet(Of String)()
Me.MySortedSet.Add("Test")
For Each item As String In Me.MySortedSet
Debug.WriteLine(item)
Next item
Me.MySortedSet.Add("Test")
MySortedSet2.Add("Test")
End Sub
End Class
You should also indicate variable types every time you declare a variable, even in a For Each statement.

Is there a function to write/export values of list-/class items?

From an external application data is extracted and written to a list with several sublist items, here is an example narrowed down to the main list creation:
Public Class clMain
Public lsLevel_1 As New List(Of clLevel_1)
End Class
Public Class clLevel_1
Public sgName As String
Public lsLevel_2 As New List(Of clLevel_2)
End Class
Public Class clLevel_2
Public sgName As String
Public lsLevel_3 As New List(Of clLevel_3)
End Class
Public Class clLevel_3
Public sgName As String
Public sgComment As String
End Class
Module Code
Sub Main()
Dim lv_clMain As clMain = New clMain
For lv_i16Level_1 = 10 To 11
Dim lv_clLevel_1 As New clLevel_1
lv_clLevel_1.sgName = "Level 1: " & lv_i16Level_1
For lv_i16Level_2 = 20 To 21
Dim lv_clLevel_2 As New clLevel_2
lv_clLevel_2.sgName = "Level 2: " & lv_i16Level_2
For lv_i16Level_3 = 30 To 31
Dim lv_clLevel_3 As New clLevel_3
lv_clLevel_3.sgName = "Level 3: " & lv_i16Level_3
lv_clLevel_2.lsLevel_3.Add(lv_clLevel_3)
Next
lv_clLevel_1.lsLevel_2.Add(lv_clLevel_2)
Next
lv_clMain.lsLevel_1.Add(lv_clLevel_1)
Next
End Sub
End Module
Once the list has been "filled", I would like to write/export the values to a file. What would be a good approach for this tasks? I think creating a function with lv_clMain as parameter. But as lv_clMain is not a collection type, and it can contain several entries of lsLevel_1 I am somewhat lost about the correct approach.
Thanks to user Hursey, I found a solution using XML serialisation.
Function SerializeXml(ByVal obData As Object, ByVal sgFileName As String, tyData As Type)
Dim obXmlSerializer As System.Xml.Serialization.XmlSerializer = New System.Xml.Serialization.XmlSerializer(tyData)
Dim obSW As System.IO.TextWriter = New System.IO.StreamWriter(sgFileName)
obXmlSerializer.Serialize(obSW, obData)
obSW.Close()
End Function

extract list of string from list of custom class

i have a list(of custom class)
and i want to extract a list of all 'name' String, from it, through linq
I know how to do with a loop, but i need to get it with a linear, brief linq instruction.
i've checked this help
C# Extract list of fields from list of class
but i have problem in linq correct syntax
in particular because i would like to extract a New List(Of String)
Class Student
Sub New(ByVal NewName As String, ByVal NewAge As Integer)
Name = NewName
Age = NewAge
End Sub
Public Name As String
Public Age As Integer
End Class
Public Sub Main
Dim ClassRoom as New List(Of Student) From {New Student("Foo",33), New Student("Foo2",33), New Student("Foo3",22)}
Dim OneStudent as Student = ClassRoom(0)
Dim AllStudentsNames As New List(Of String) From {ClassRoom.Select(Function(x) x.Name <> OneStudent.Name).ToList}
End Sub
But something wrong...
Any help?
P.S. Since c# it's close to vb.Net, also c# helps are well welcome.
First, you don't need to create a new list From the one returned by the LINQ method. It's already in a new list at that point, so you can just set AllStudentsNames equal directly to what the ToList method returns.
Second, you are not selecting the name. You are selecting the result of the equality test to see if the names are different. In other words, when you say Select(Function(x) x.Name <> OneStudent.Name), that returns a list of booleans, where they true if the names are different and false if the names are the same. That's not what you want. You want the list of names, so you need to select the name.
Third, if you need to filter the list so that it only returns ones where the name is different, then you need to add a call to the Where method.
Dim AllStudentsNames As List(Of String) = ClassRoom.
Where(Function(x) x.Name <> OneStudent.Name).
Select(Function(x) x.Name).
ToList()

Readonly in keyvaluepair

Well I have created a program that takes some files (Mp3) and change their tags
recently I wanted to add some new Subs (like: Take the songs name and make every letter in it upercase). The problem is that i use a list with its items to be keyvaluepairs
Public MP3List As New List(Of KeyValuePair(Of String, String))
When i tried to edit the key or value of any Item in that list i get an error (That this is READONLY)
Example:
For Each Song In MP3List
Song.Key = "Something"
Next
I add items like this :
Private Function OpenAFile()
Dim MP3List1 = MP3List
Dim oFileDialog As New OpenFileDialog
oFileDialog.Title = "Επέλεξε ένα MP3 Άρχειο"
oFileDialog.Filter = "MP3 Files|*.mp3|All Files|*.*"
oFileDialog.Multiselect = True
Dim Path As String = ""
Dim Name As String = ""
Dim NewPair As New KeyValuePair(Of String, String)
If oFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
For Each sPath In oFileDialog.FileNames
Path = New String(sPath)
Name = New String(Strings.Split(Path, "\").ToList(Strings.Split(Path, "\").ToList.Count - 1))
NewPair = New KeyValuePair(Of String, String)(Name, Path)
If Not MP3List1.Contains(NewPair) Then MP3List1.Add(NewPair)
Next
End If
Return MP3List1
End Function
So the idea is this: Each time i press A button to add a song it will run the function OpenAFile() and it was working fine then . Now that i want to change a key or value i get this error
Thanks for the Help and sorry for bad english
The Keys in a KeyValuePair are readonly because they are often used as the key in a hash table. Changing the key would cause issues where you would lose your item in the hash.
If you want to do something like this, you could always create your own data type that stores a key and value. An overly simplified example would be as follows.
Public Structure PathNamePair
Public Property Path As String
Public Property Name As String
Public Sub New(path As String, name As String)
Me.Path = path
Me.Name = name
End Sub
End Structure
I will note that in order to get better performance with your Contains method, you should also implement IEquatable(Of T), but that's probably beyond the scope of this question. I will also note that it is not best practice to have a ValueType (Structure) that is mutable.

List (Of T) as DataGridView.DataSource makes sorting fail

I have read some threads about this "error" but I can't figure out how to solve my problem.
I have a class that looks something like this:
Public Class Person
Public Property Name As String
Public Property PhoneNumber As string
Public Property Age As Integer
Public sub New(ByVal Values As String())
Me.Name = Values(0)
Me.PhoneNumber = Values(1)
Me.Age = Convert.ToInt32(Values(2))
End Sub
End Class
I get my data from a semicolon separated file, and i create a list of Person objects by looping this file and split on semicolon. Like this
Dim PersonsList As New List(Of Person)
For Each line in textfile..........
PersonsList.Add(New Person(line.Split(";")))
Next
When the list is complete, I tell my DataGridView that DataSource is PersonsList.
This works like a charm, but I'm not able to sort the columns.
I found this post amongst many (where the class values are not properties, which mine are) and tried that converting function which did'nt really work in my case. The right amount of rows were created, but all of the columns were blank.
What am I missing?
If you use a datatable as the data source, column sorting is automatically enabled and you can sort the data by any column:
Dim dt As New DataTable
dt.Columns.AddRange(
{
New DataColumn("Name"),
New DataColumn("Phone"),
New DataColumn("Age")
})
For Each s As String In IO.File.ReadAllLines("textfile1.txt")
Dim temprow As DataRow = dt.NewRow
temprow.ItemArray = s.Split(";"c)
dt.Rows.Add(temprow)
Next
DataGridView1.DataSource = dt