To pass a whole array by reference into a function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sandeeps123
    New Member
    • Feb 2007
    • 4

    To pass a whole array by reference into a function

    I have an array s(0) to s(63).
    I want to perform an operation: text1(s(i)).bac kcolor=vbblue.
    Now Textbox1 is a control array: text1(0) to text1(63).
    I need to pass S(0) to s(63)...all in a function by reference.
    I tried it just by putting byref s in the function...but it doesnt work, ie the values of s(0) to s(63) do not change, they remain the same.
    Does anybody have a solution to this problem?
  • balid
    New Member
    • Feb 2007
    • 18

    #2
    Originally posted by sandeeps123
    I have an array s(0) to s(63).
    I want to perform an operation: text1(s(i)).bac kcolor=vbblue.
    Now Textbox1 is a control array: text1(0) to text1(63).
    I need to pass S(0) to s(63)...all in a function by reference.
    I tried it just by putting byref s in the function...but it doesnt work, ie the values of s(0) to s(63) do not change, they remain the same.
    Does anybody have a solution to this problem?
    Why do you need to actually pass the values? Couldn't your function just look at the value of s(i) and apply the appropriate backcolor?

    May be with more information on your function, need, and limits, I may have a better idea for you.

    Thanks.

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Originally posted by sandeeps123
      I have an array s(0) to s(63).
      I want to perform an operation: text1(s(i)).bac kcolor=vbblue.
      Now Textbox1 is a control array: text1(0) to text1(63).
      I need to pass S(0) to s(63)...all in a function by reference.
      I tried it just by putting byref s in the function...but it doesnt work, ie the values of s(0) to s(63) do not change, they remain the same.
      Does anybody have a solution to this problem?
      From what you have told us here, I would not expect the values in the S array to change. All you are doing with it is using it as an indirect pointer to the elements in the Text1 control array.

      Perhaps it would help if you post the actual code.

      P.S. ByRef is the default passing type anyway, so adding it to the declaration doesn't really make any difference.

      Comment

      • sandeeps123
        New Member
        • Feb 2007
        • 4

        #4
        Well, actually what happens is that there is a timer control and with each interval of the timer and the command button the user presses, the values of s(i) are to be modified and to those values of s(i), text1(s(i)).bac kcolor should be changed.
        So the important thing is that s(i) keeps changing and only to those s(i) values these changes are to be applied!
        Thanks for ur help...looking forward to getting more help from you!!!


        Originally posted by balid
        Why do you need to actually pass the values? Couldn't your function just look at the value of s(i) and apply the appropriate backcolor?

        May be with more information on your function, need, and limits, I may have a better idea for you.

        Thanks.

        Comment

        • sandeeps123
          New Member
          • Feb 2007
          • 4

          #5
          The code would be too long to post here. So I'm telling what I want to do precisely:
          I'm designing a code for Snakes game in vb and I'm using textboxes text1(0) to text1(63). To change the appearence of the snake, I'm using the property text1(s(i)).bac kcolor=vbblue.. ...so that I keep changing the values of s(i) and send these values of s(i) into the appropriate functions after each interval of the timer.
          And I din't understand when u said byref is not going to make any difference.

          Thnks for the help...looking forward to more help

          Originally posted by Killer42
          From what you have told us here, I would not expect the values in the S array to change. All you are doing with it is using it as an indirect pointer to the elements in the Text1 control array.

          Perhaps it would help if you post the actual code.

          P.S. ByRef is the default passing type anyway, so adding it to the declaration doesn't really make any difference.

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Ok, I think I get the idea of what you're doing.

            As for the mention of ByRef - there are two options for passing a variable to a sub or function. They are by value (ByVal) or by reference (ByRef). The default is to pass by reference, so when you add the keyword ByRef, it actually doesn't make any difference.

            As for the original question, passing an array to a Sub or Function is quite simple. Here's an example.
            Code:
            Private Sub Something([B]s() As String[/B])
              ...
            End Sub
            To invoke this, just pass your array like so:
            Code:
            Dim MyArray(1 to 20) As String
            Something [B]MyArray[/B]

            Comment

            • sandeeps123
              New Member
              • Feb 2007
              • 4

              #7
              Well, I'm sorry to say this but I did not understand the code you have written.
              Please explain the code, what does this code do?

              Private Sub Something(s() As Integer)
              End Sub

              and this one?

              Dim MyArray(1 To 63) As String
              Something s(40)

              Which is the array and the function out of these? Where can I write the code for the function?


              Originally posted by Killer42
              Ok, I think I get the idea of what you're doing.

              As for the mention of ByRef - there are two options for passing a variable to a sub or function. They are by value (ByVal) or by reference (ByRef). The default is to pass by reference, so when you add the keyword ByRef, it actually doesn't make any difference.

              As for the original question, passing an array to a Sub or Function is quite simple. Here's an example.
              Code:
              Private Sub Something([B]s() As String[/B])
                ...
              End Sub
              To invoke this, just pass your array like so:
              Code:
              Dim MyArray(1 to 20) As String
              Something [B]MyArray[/B]

              Comment

              • nmadct
                Recognized Expert New Member
                • Jan 2007
                • 83

                #8
                What version of Visual Basic are you using? It matters a lot in this case, because VB6 and earlier versions pass array parameters differently from the way VB.NET does it.

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #9
                  Originally posted by sandeeps123
                  Well, I'm sorry to say this but I did not understand the code you have written.
                  Please explain the code, what does this code do?

                  Private Sub Something(s() As Integer)
                  End Sub

                  and this one?

                  Dim MyArray(1 To 63) As String
                  Something s(40)

                  Which is the array and the function out of these? Where can I write the code for the function?
                  We are here to help those who have difficulties with their code. But I think you really need to learn at least some basics of how to write code before asking - this is not a school.

                  Here are a couple of pointers, though. I'm going to assume that you are using VB6.

                  To define a sub:
                  Code:
                  Private Sub [I]YourNameHere[/I](parameter definitions go here)
                   [b]or[/b]
                  Public Sub [I]YourNameHere[/I](parameter definitions go here)
                  To define a function:
                  Code:
                  Private Function [I]YourNameHere[/I](parameter definitions go here) As [I]ReturnType[/I]
                   [b]or[/b]
                  Public Function [I]YourNameHere[/I](parameter definitions go here) As [I]ReturnType[/I]
                  To call them, it's just the same as any of the built-in statements and functions.

                  Comment

                  Working...