Reputation: 37
I have a List of String:
Dim values as List(Of String) = {{1 ; 4} , {2 ; 8} , {3 ; 16}}
This list was created by a previous existing code (so I didn't define it as a list of strings by myself).
So when I type
MsgBox(values(0))
'Output: 1 ; 4
this shows up.
Those values correspond to xy-Values, so what I want to have is something like this
MsgBox(xVal(0))
'Output: 1
MsgBox(yVal(2))
'Output: 16
I have been looking for a solution for quite a while now, scince VB.net is quite new for me. The main problem here is the conversion between the types. Would appreciate any kind of help, thank you.
Upvotes: 0
Views: 4225
Reputation: 39122
Those values correspond to xy-Values
Here's an example using ConvertAll to get a List
of Point:
Dim values As New List(Of String)({"1 ; 4", "2 ; 8", "3 ; 16"})
Dim coords As List(Of Point) = values.ConvertAll(Of Point)(Function(ByVal input As String)
Dim data() As String = input.Split(";")
Return New Point(CInt(data(0)), CInt(data(1)))
End Function)
For Each pt As Point In coords
Debug.Print(pt.X & ", " & pt.Y)
Next
Output:
1, 4
2, 8
3, 16
I didn't include any error checking on the data. It assumes each string has a semi-colon and that the elements in the first two positions of the resulting array are valid integers.
Upvotes: 2
Reputation: 655
You can use the split function :
MsgBox(values(0).Split(";")(0).Trim) 'For the X in values(0)
MsgBox(values(0).Split(";")(1).Trim) 'For the Y in values(0)
Split(";") will split your string in an array with the ";" as delimiter.
https://learn.microsoft.com/fr-fr/dotnet/api/microsoft.visualbasic.strings.split?view=net-5.0
Trim is used to remove spaces :
Dim a_string as string = " 7 "
MsgBox(a_string.Trim) 'Output : "7"
In your example :
MsgBox(values(0).Split(";")(0).Trim)
'Output: 1
MsgBox(values(2).Split(";")(1).Trim)
'Output: 16
Upvotes: 0
Reputation: 1559
You can convert the List to x, y array like below
Dim xval as new List(Of Integer)
Dim yVal as new List(Of Integer)
For Each v in values
xval.add(v.Split(";")(0))
yval.add(v.Split(";")(1))
Next
' Prinitng as you tested,
Console.WriteLine(xval(0))
Console.WriteLine(yval(2))
you can use the xVal, yVal arrays for processing your business logic
Upvotes: 0