0

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.

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

thank you! First, it didn't work for me. But when I change xVal and yVal to a List of Object, everything is fine. Otherwise it says "System.OverflowException" for whatever reason
0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.