var_dump or alternative?

If you want to look at the contents of a an object you need to look at it in the debugger. Is there anyway to dump the contents of a object to the debug window as text ( like var_dump() in php )?

uh no … :slight_smile:

Thanks @Norman Palardy

How about… Introspection?

Just a quick and dirty test/example (one could extend that obviously even more, e.g. recursively processing object properties, …)
Add a public Module with this Method:

[code]Public Function var_dump(Extends o As Object) as String
Dim s() As String

Dim t As Introspection.TypeInfo = Introspection.GetType(o)
s.Append(“[” + t.Name + “]”)

Dim ps() As Introspection.PropertyInfo = t.GetProperties
Dim v As Variant
For Each p As Introspection.PropertyInfo In ps
v = p.Value(o)
select case v.Type
case Variant.TypeObject
s.Append(“- " + p.Name + " → (an object)”)
else
try
s.Append(“- " + p.Name + " → " + v.StringValue)
catch TypeMismatchException
s.Append(”- " + p.Name + " → (??? type mismatch exception)")
end try
end select
Next

return Join(s, EndOfLine)
End Function[/code]

Use it like this:

Dim f As New FolderItem System.DebugLog(f.var_dump)

In the Debug-Messages you then see (or just use the string to save to a file or “dump” where-ever you want it):

[code][FolderItem]

  • AbsolutePath → Cadmium SSD:Users:juerg:Desktop:
  • Alias → False
  • Count → 4
  • Visible → True[/code]

Yes, @Jürg Otter - exactly what I was thinking. I like ‘dumping’ objects to a readable window… I find picking through the debugger list a bit sluggish. I can see you’ve done some coding on this- thanks! happy to try this out.

Just a really quick attempt. Obviously not quite the same as var_dump in PHP…
But for the “object - dump” you’ve asked for, and if with “contents” you’ve meant the properties, then it might just be enough (depends on what you want/need to “see”). And one can always do even more if needed… :wink:

Somehow the Xojo IDE populates the Debugger-infos - I don’t know how, but I could imagine it’s doing that using Introspection…
…but maybe we get another “uh, no” for that assumption from @Norman Palardy :stuck_out_tongue:

uh no comment :stuck_out_tongue:

I’ve just been curious how one could do something a bit more similar to PHP’s var_dump.

Here’s another a quick and dirty test… well, example project: VarDump

What I’ve tried:

  • use a ParamArray to allow dumping as many variables as you like: var_dump(a, b, c)
  • to have a “named output” for the variables: var_dump_pair("My Variable a" : a, "b" : b, "c" : c)
  • use Xojo’s JSONItem to do most of the output-formatting

Again: quick and dirty. It probably can’t cope with every language feature (I haven’t even tried or tested Delegates, Pointers, Interfaces, Object-Arrays, …).

If someone looks at this example, I’d be curious to know if it’s possible to have the overloaded Sub var_dump(ParamArray vDumps() As Variant) hand over the ParamArray to the other Function var_dump(ParamArray vDumps() As Variant) as String as a ParamArray…

…because this will then hand over a Variant-Array (and not a Param Array):

Public Sub var_dump(ParamArray vDumps() As Variant) System.DebugLog(var_dump(vDumps)) End Sub

I don’t quite like that duplicated code (as said… a quick and dirty example), but I currently don’t have a good idea how to “forward a ParamArray” right now :wink:

this decl looks wrong

Public Sub var_dump(ParamArray vDumps() As Variant)

this implies a param array of arrays
Is that what you meant ?

probably needs to be

Public Sub var_dump(ParamArray vDumps As Variant)

and vDumps is still treated an array

and no forwarding the paramarray to a function declared as “vDumps() as variant” doesnt work
<https://xojo.com/issue/20164>