How can I gracefully tell if a variable is initialized or not and handle it appropriately?
I'm modifying the example "Video Player" code which has a function in generalUtils.brs like so:
'******************************************************
'Walk an AA and print it
'******************************************************
Sub PrintAA(aa as Object)
print "---- AA ----"
if aa = invalid
print "invalid"
return
endif
mytype = type(aa)
'TODO: not sure how to do this in Brightscript
'else if mytype = "<uninitialized>"
' print "uninitialized"
' return
if mytype = "roAssociativeArray"
cnt = 0
for each e in aa
x = aa[e]
PrintAny(0, e + ": ", aa[e])
cnt = cnt + 1
next
if cnt = 0
PrintAny(0, "Nothing from for each. Looks like :", aa)
endif
else
print mytype
endif
print "------------"
End Sub
And in the appPosterScreen.brs I'm trying to do this:
myshows = getShowsForCategoryItem(category, m.curCategory)
PrintAA(myshow)
'PrintList(myshow)
screen.SetContentList(myshows)
screen.Show()
And it STB every time I try to output that as either PrintAA or PrintList:
created feed connection for http://dev.roku.example.com/xml/new_scenes_today.xml
url: http://dev.roku.example.com/xml/new_scenes_today.xml
Request Time: 18
Show Feed Parse Took : 60
---- AA ----
BrightScript Micro Debugger.
Enter any BrightScript statement, debug commands, or HELP.
Current Function:
440: Sub PrintAA(aa)
441: print "---- AA ----"
442: if aa = invalid
443: print "invalid"
444: return
445: endif
446: mytype = type(aa)
447: 'TODO: not sure how to do this in Brightscript
448: 'else if mytype = "<uninitialized>"
449: ' print "uninitialized"
450: ' return
451: if mytype = "roAssociativeArray"
452: cnt = 0
453: for each e in aa
454: x = aa[e]
455: PrintAny(0, e + ": ", aa[e])
456: cnt = cnt + 1
457: next
458: if cnt = 0
459: PrintAny(0, "Nothing from for each. Looks like :", aa)
460: endif
461: else
462: print mytype
463: endif
464: print "------------"
465: End Sub
Use of uninitialized variable. (runtime error &he9) in .../pkg:/source/generalUtils.brs(442)
442: if aa = invalid
Backtrace:
Function printaa(aa As ) As
file/line: /tmp/plugin/FIAAAAnKZ.../pkg:/source/generalUtils.brs(442)
Function showposterscreen(screen As <uninitialized>, category As <uninitialized>) As Integer
file/line: /tmp/plugin/FIAAAAnKZLFS/pkg:/source/appPosterScreen.brs(57)
Function displaycategoryposterscreen(category As <uninitialized>) As
file/line: /tmp/plugin/FIAAAAnKZLFS/pkg:/source/appHomeScreen.brs(81)
Function showhomescreen(screen As ) As Integer
file/line: /tmp/plugin/FIAAAAnKZLFS/pkg:/source/appHomeScreen.brs(56)
Function main() As
file/line: /tmp/plugin/FIAAAAnKZLFS/pkg:/source/appMain.brs(19)
Local Variables:
aa &h0000 <uninitialized> val:Uninitialized
global &h0020 rotINTERFACE:ifGlobal
m &h0010 bsc:roAssociativeArray, refcnt=6
mytype &h0000 <uninitialized> val:Uninitialized
cnt &h0000 <uninitialized> val:Uninitialized
e &h0000 <uninitialized> val:Uninitialized
x &h0000 <uninitialized> val:Uninitialized
BrightScript Debugger>
Isn't there just a simple way to output what a variable is (like in PHP there is
var_dump($aa))?!
I know "myshows" has stuff in it, as without the PrintAA/PrintList the page renders fine and no error happens. So why does it say "uninitialized"?