locked
Trying to graduate and run a program RRS feed

  • Question

  • I'm trying to graduate the following program:

    rows = 8
    columns = 8
    size = 40
    For r = 1 To rows
      For c = 1 To columns
        GraphicsWindow.BrushColor = GraphicsWindow.GetRandomColor()
        boxes[r][c] = Shapes.AddRectangle(size, size)
        Shapes.Move(boxes[r][c], c * size, r * size)
      EndFor
    EndFor

    It gets the following error in Visual Basic:

    Error 3 Expression is a value and therefore cannot be the target of an assignment. C:\Documents and Settings\Jim Rice\My Documents\My Project\UntitledModule.vb 12 17 Untitled

    The expression that it is talking about is an array of boxes.  Question is:  What do I need to do to make this run?

     

    Wednesday, August 11, 2010 5:27 PM
    Answerer

Answers

  • Visual Basic don't like your original boxes array, but here is your code.

    Imports Microsoft.SmallBasic.Library


    Module UntitledModule

        Dim rows, columns, size, r, c, boxes, subBoxes As Primitive

        Sub Main()

            rows = 8

            columns = 8

            size = 40

            For r = 1 To rows

                For c = 1 To columns

                    GraphicsWindow.BrushColor = GraphicsWindow.GetRandomColor()
                    subBoxes(c) = Shapes.AddRectangle(size, size)
                    boxes(r) = subBoxes

                    Shapes.Move(boxes(r)(c), c * size, r * size)
                Next

            Next

        End Sub
    End Module


    Sorry My Bad English
    • Proposed as answer by Oskariok Wednesday, August 11, 2010 8:25 PM
    • Marked as answer by jricestkEditor Wednesday, August 11, 2010 9:44 PM
    Wednesday, August 11, 2010 8:25 PM
  • Small Basic has trouble converting arrays, and you must declare them manually. Instead of boxes As Primitive, use
    Dim boxes(8, 8) As Primitive

    If your not sure of how large your array will be, you can also declare them without bounds, as in
      Dim boxes(,) As Primitive
    and later in your program use ReDim when you know the bounds
      ReDim boxes(rows, columns)

    Module UntitledModule
     Dim rows, columns, size, r, c As Primitive
     Dim boxes(,) As Primitive 'declare a multidimensional array without bounds
     'this could also be -- Dim boxes(8, 8) As Primitive
    
     Sub Main()
     rows = 8
     columns = 8
     ReDim boxes(rows, columns) 'now that we know the dimensions, use ReDim 
     size = 40
     For r = 1 To rows
      For c = 1 To columns
      GraphicsWindow.BrushColor = GraphicsWindow.GetRandomColor()
      boxes(r, c) = Shapes.AddRectangle(size, size)
      Shapes.Move(boxes(r, c), c * size, r * size)
      Next
     Next
    
     End Sub
    End Module
    
    
    • Marked as answer by jricestkEditor Thursday, August 12, 2010 2:03 AM
    Thursday, August 12, 2010 1:06 AM

All replies

  • Why do you want it in Visual Basic? I'm just wondering what the point of 'graduating' is.
    Wednesday, August 11, 2010 5:37 PM
  • Can you send your code? It is hard to say without VB code.

    Sorry My Bad English
    Wednesday, August 11, 2010 5:46 PM
  • Oskariok,

    Here's the code that is made by visual basic.  Get's the error on the line that shows "boxes (r) (c)".  I don't understand the error or what to do to fix it.

    Module

     

     

    UntitledModule

     

     

    Dim rows, columns, size, r, c, boxes As Primitive

     

     

     

    Sub Main()

    rows = 8

    columns = 8

    size = 40

     

     

    For r = 1 To rows

     

     

    For c = 1 To columns

     

     

    GraphicsWindow.BrushColor = GraphicsWindow.GetRandomColor()

    boxes(r)(c) =

     

    Shapes.AddRectangle(size, size)

     

     

    Shapes.Move(boxes(r)(c), c * size, r * size)

     

     

    Next

     

     

    Next

     

     

    End Sub

    End

     

     

    Module

    JRicestk

    Wednesday, August 11, 2010 7:38 PM
    Answerer
  • ak9779,

    I'm trying to learn how to successfully graduate code to the visual basic level.

    Jricestk 

    Wednesday, August 11, 2010 7:41 PM
    Answerer
  • Visual Basic don't like your original boxes array, but here is your code.

    Imports Microsoft.SmallBasic.Library


    Module UntitledModule

        Dim rows, columns, size, r, c, boxes, subBoxes As Primitive

        Sub Main()

            rows = 8

            columns = 8

            size = 40

            For r = 1 To rows

                For c = 1 To columns

                    GraphicsWindow.BrushColor = GraphicsWindow.GetRandomColor()
                    subBoxes(c) = Shapes.AddRectangle(size, size)
                    boxes(r) = subBoxes

                    Shapes.Move(boxes(r)(c), c * size, r * size)
                Next

            Next

        End Sub
    End Module


    Sorry My Bad English
    • Proposed as answer by Oskariok Wednesday, August 11, 2010 8:25 PM
    • Marked as answer by jricestkEditor Wednesday, August 11, 2010 9:44 PM
    Wednesday, August 11, 2010 8:25 PM
  • Oskariok,

    Thank you for solving the problem.  It looks like you split the multi-dimensional array into 2 single arrays.  Any idea of why the multi-dimensional didn't work?

    Jricesk

    Wednesday, August 11, 2010 9:46 PM
    Answerer
  • Small Basic has trouble converting arrays, and you must declare them manually. Instead of boxes As Primitive, use
    Dim boxes(8, 8) As Primitive

    If your not sure of how large your array will be, you can also declare them without bounds, as in
      Dim boxes(,) As Primitive
    and later in your program use ReDim when you know the bounds
      ReDim boxes(rows, columns)

    Module UntitledModule
     Dim rows, columns, size, r, c As Primitive
     Dim boxes(,) As Primitive 'declare a multidimensional array without bounds
     'this could also be -- Dim boxes(8, 8) As Primitive
    
     Sub Main()
     rows = 8
     columns = 8
     ReDim boxes(rows, columns) 'now that we know the dimensions, use ReDim 
     size = 40
     For r = 1 To rows
      For c = 1 To columns
      GraphicsWindow.BrushColor = GraphicsWindow.GetRandomColor()
      boxes(r, c) = Shapes.AddRectangle(size, size)
      Shapes.Move(boxes(r, c), c * size, r * size)
      Next
     Next
    
     End Sub
    End Module
    
    
    • Marked as answer by jricestkEditor Thursday, August 12, 2010 2:03 AM
    Thursday, August 12, 2010 1:06 AM
  • Rushworks,

    Your reply is what I needed.  I played around with Dim when I was trying to get it to work and never thoght about redim.

    Thank you,

    Jricestk

    Thursday, August 12, 2010 2:06 AM
    Answerer