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

KevinKZ
  • 129
  • 1
  • 1
  • 7

3 Answers3

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()
Fabio
  • 31,528
  • 4
  • 33
  • 72
  • 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 – KevinKZ Jul 04 '14 at 16:02
  • Didn't post an answer because i know nothing of Visual Basic. (Sidenote: LINQ seems odd in Visual Basic) – Machinarius Jul 04 '14 at 16:32
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.

JoelC
  • 3,664
  • 9
  • 33
  • 38
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.

user3740891
  • 33
  • 2
  • 4
  • 11
  • I've seen plenty of examples online that do this such as this one: http://stackoverflow.com/questions/751881/convert-list-of-string-to-a-string-separated-by-a-delimiter Why isn't mine working? – KevinKZ Jul 04 '14 at 15:34
  • 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! – user3740891 Jul 04 '14 at 15:39
  • 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))` – Ňɏssa Pøngjǣrdenlarp Jul 04 '14 at 15:41
  • @user3740891 I used this online tool to convert the json to DatContract types [link](http://jsontodatacontract.azurewebsites.net/) and `g.board.tiles` is defined as `Public Property tiles() As List(Of List(Of String))` where 'Board' is a class – KevinKZ Jul 04 '14 at 15:42