Search code examples
xojo

xojo call a method with button


I have the code below in a View method to parse JSON files. The method ContentsOfDictionary accepts the parameters d as Xojo.Core.Dictionary and level as an integer, and returns text.
If I use ContentsOfDictionary in an action button, I get this error:

error: Not Enough arguments: got 0, expected at least 1

Here is a link to the test file I am using.
How do I call the method ContentsOfDictionary?

 dim dict as Xojo.Core.Dictionary = Xojo.Data.ParseJSON (kJsonData)
   const kEOL = &u0A

  dim indent as text
  for i as integer = 1 to level
    indent = indent + " "
  next
  dim t as text
  for each entry as Xojo.Core.DictionaryEntry in dict
    t = t + indent + entry.Key + " "
    if Xojo.Introspection.GetType( entry.Value ) is nil then
      t = t + "nil" + kEOL
    else
      select case Xojo.Introspection.GetType( entry.Value ).Name
      case "Boolean"
        t = t + if( entry.Value, "true", "false" ) + kEOL
      case "Text"
        t = t + entry.Value + kEOL
      case "Int32", "Int64"
        //
        // You get the idea
        //
      case "Dictionary"
        t = t  + kEOL + ContentsOfDictionary( entry.Value, level + 1 )
      end select
    end if
  next
  return t

Solution

  • UPDATE: None of the below applies as @johnnyB's example was of the ContentsOfDictionary function and should not have been trying to access the JSON data but just working on the supplied Dictionary. There is a link to the fixed test project in the comments below.


    It's failing because you're not satisfying the parameter types of any of ContentsOfDictionary's signatures.

    entry.Value is of type Auto, so before calling ContentsOfDictionary you need to copy entry.Value into a Dictionary and pass that in to the function instead.

    For example...

        ...
        case "Dictionary"
            dim d as new Xojo.Core.Dictionary = entry.Value
            t = t  + kEOL + ContentsOfDictionary( d, level + 1 )
    end select