0

Brightscript array of objects

Hi,

I know enough object oriented programming to be dangerous but that's all.  I'm trying to programmatically write to multiple files using Brightscript.  What I'd like to do is open multiple files and write several strings to each one.  Something like this:

 

file[index] = CreateObject(roCreateFile, "filename[index]")

fileWriter[index] = CreateObject(roReadWriteFile, "filename[index]")

and then use the fileWriter objects to write various strings to each of the files using .SendLine.

How do I do that in Brightscript?

Thanks,

Tony

8 comments

  • 0
    Avatar
    ^UD\_$

    Looks like you figured it out more or less.

    Assuming that files are 1.txt, 2.txt,... <n>.txt

    Pseudo code:

     

    Files = CreateObject("roArray")

    http://docs.brightsign.biz/display/DOC/roArray

     

    ' Creating files

    for index 1 to NumberOfFiles

    Files.Push(CreateObject(roCreateFile, index.ToStr()+".txt")

    Next 

     

     

    To reference file 3.txt for example, you would use object Files[3]

     

    if thatWasHelpful = true then

    forum.LeaveComment() 

    else

    forum.LeaveCommentAnyway()

    end if

  • 0
    Avatar
    Marino, Tony

    Thank you Udi that was helpful.

  • 0
    Avatar
    Marino, Tony

    Hi Udi,

    Still having issues.  Here's the simple script:

    sub main()
     outfile = CreateObject("roArray")

     print "Starting up..."
     for ind = 1 to 5
      outfile.Push(CreateObject("roCreateFile", "File_" + strI(ind) + ".txt")
      print "Index ", ind
     end for
     
    end sub

    It hangs up on the outfile.Push statement.  Any ideas what could be wrong?

  • 0
    Avatar
    ^UD\_$

    Try this

     

     for ind% = 1 to 5 ' define ind% as integer

    FileName$ = "File_" + strI(ind%) + ".txt"  ' Make sure it is strI (with a capital i, and not lower case L)

    Print "File Name: " + FileName$
      outfile.Push(CreateObject("roCreateFile", FileName$)
      print "Index ", ind%
     end for ' This may need to be next, instead of end for...

     

    Are you coding a BrightAuthor plugin, or is it a custom autorun?

    You should become familiar with the serial port console, which gives you access to the debugger.

    The debugger can save you some time when experimenting with brightscript.

  • 0
    Avatar
    Marino, Tony

    Hi Udi,

    I'm doing a custom autorun.

    Still no luck.  Debugger says outfile is invalid:

    BrightScript Debugger> outfile.Push(CreateObject("roCreateFile", "File_1.txt"))
    'Dot' Operator attempted with invalid BrightScript Component or interface reference. (runtime error &hec) in $LIVECOMPILE(14)

  • 0
    Avatar
    ^UD\_$

    Hi Tony,

     

    How have you defined outfile.

    It doesn't seem to be a roArray.

    http://docs.brightsign.biz/display/DOC/roArray

     

    Can you share the entire code here?

  • 0
    Avatar
    Marino, Tony

    I found it.  outfile needs to be defined as:

     outfile = CreateObject("roArray", 5, 1)

    Thanks for your help!

  • 0
    Avatar
    ^UD\_$

    Your'e welcome.

     

    Does this work too?

    outfile = []

Please sign in to leave a comment.