0

I am using VS2013 and .NET FrameWork 4.0.

I am building an app that reads a json file and acts upon it.

I have successfully written the code to deserialize the json file and I have a list(of String) which I would like to join in a single string.

This is my code:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim g As GameData = Nothing


    Using fileStream = New System.IO.FileStream("C:\Users\KE-KL\Desktop\Levels\level_0017.json", System.IO.FileMode.Open)
        fileStream.Position = 0
        Dim ser = New System.Runtime.Serialization.Json.DataContractJsonSerializer(GetType(GameData))
        g = DirectCast(ser.ReadObject(fileStream), GameData)
    End Using
        Dim final As String
    final = String.Join(",", g.board.tiles.ToArray)
End Sub

But this line:final = String.Join(",", g.board.tiles.ToArray) creates this error:

Error 1 Overload resolution failed because no accessible 'Join' is most specific for these arguments: 'Public Shared Function Join(Of System.Collections.Generic.List(Of String))(separator As String, values As System.Collections.Generic.IEnumerable(Of System.Collections.Generic.List(Of String))) As String': Not most specific. 'Public Shared Function Join(separator As String, ParamArray values() As Object) As String': Not most specific

Any idea how to fix this? If you need more details, please do ask. Thank you in advance

6
  • ToArray should be ToArray() as it is a method. Commented Jul 4, 2014 at 15:29
  • 2
    @adaam in vb.net not neccesarry Commented Jul 4, 2014 at 15:32
  • 1
    What type is g.board.tiles property is? Commented Jul 4, 2014 at 15:38
  • @Fabio Public Property tiles() As List(Of List(Of String)) Commented Jul 4, 2014 at 15:38
  • Try using linq and aggregating the string list [stackoverflow.com/questions/7105505/… Commented Jul 4, 2014 at 15:42

3 Answers 3

2

As your error message said: you trying to pass array of List(Of String) to array of Objects
Try as @Michinarius advised use Aggregate method:

final = g.board.tiles.Aggregate(Of StringBuilder)(New StringBuilder(), _
                                                 Function(temp, val)
                                                     temp.Append(String.Join(",", val))
                                                     Return temp
                                                 End Function).ToString()
Sign up to request clarification or add additional context in comments.

2 Comments

YESSSS! Thank youuu... The first method returned 9 lists but the second method actually worked and it returned every single element such as 'X', 'E', 'A' etc. Thank you very much. I have been banging my head to the wall for the past day. Chosen as solution
Didn't post an answer because i know nothing of Visual Basic. (Sidenote: LINQ seems odd in Visual Basic)
1

Your problem is that a List(Of List(Of String)) will convert to a multi-dimensional array with the ToArray() call, which String.Join does not handle. Since you have a list of lists, you could do String.Join(",", g.board.titles(0)) and that should work.

Also note that I didn't need the ToArray() call because one of the overrides for join takes an IEnumerable(Of T), which List(Of T) implements.

Comments

0

I think this is because you are trying to join a String (",") and an array (g.board.tiles.ToArray). The Join method doesn't accept String and Array arguments. Choose one or the other, and remember to include an index (or multiple indices for multidimensional arrays) when dealing with specific parts of arrays.

4 Comments

I've seen plenty of examples online that do this such as this one: stackoverflow.com/questions/751881/… Why isn't mine working?
What exactly is g.board.tiles? What kind of format is it in? If it's not natively an array or in some other way not designed for this purpose, that might be why it's not working. Otherwise, I'm stumped!
If tiles was truly a list(of string) then the code should work. .ToArray would feed the list to String.Join as a param array. It is now apparently a List(Of List(of String))
@user3740891 I used this online tool to convert the json to DatContract types link and g.board.tiles is defined as Public Property tiles() As List(Of List(Of String)) where 'Board' is a class

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.