Tech Off Thread

4 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

"Seeing" array value in another sub

Back to Forum: Tech Off
  • User profile image
    mancroft

    Array value in another sub

    Hello

    I am having trouble getting another sub to "see"  the contents of an array created elsewhere.

    I have set up an array thus: [cut-down code]

    [code]
    Public thetextbox() As TextBox

    private sub setuparray()
     
    Dim thetextbox(number) As TextBox

    For intCounter As Integer = 0 To Length)

     thetextbox(intCounter) = New TextBox
    thetextbox(intCounter).Location = New System.Drawing.Point(560, spaceCounter)
    thetextbox(intCounter).Size = New System.Drawing.Size(35, 16)
    thetextbox(intCounter).Font = New System.Drawing.Font("Verdana", 8.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    thetextbox(intCounter).Name = "thetextbox" & intCounter
    thetextbox(intCounter).Text =  intCounter

    Panel1.Controls.Add(thetextbox(intCounter))


    next

    end sub
    [/code]

    How do I show the value of  thetextbox(3) in the sub below?

    [code]
    private sub showpartofarray()

    'how to show contents of thetextbox(3)

    end sub
    [/code]

    Dim strDisplay As String= thetextbox(3).Text
    gives the error: "Object reference not set to.... etcetera"

    Thank you

  • User profile image
    Maurits

    mancroft wrote:

    Public thetextbox() As TextBox

    private sub setuparray()
     
    Dim thetextbox(number) As TextBox



    I think that Dim line is the problem.  You're creating a second array inside the scope of the setuparray function.  When the setuparray function closes, that array goes out of scope and you're left with only the original array.  You probably want ReDim instead.

  • User profile image
    out180

    Yeah, like Mauris said, the second Dim statement inside the SetupArray() method should be:

    ReDim theTextBox(number)

    Also notice there's no 'as <type>' suffix inside the SetupArray method.

  • User profile image
    mancroft

    Thanks guys!

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.