Results 1 to 4 of 4

Thread: [RESOLVED] Variable not retaining value when exiting sub.

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    816

    Resolved [RESOLVED] Variable not retaining value when exiting sub.

    Hi,
    Can someone explain why a public variable is not retaining it's value when exiting a subroutine.

    I want to search through an array and if a condition is hit then the subroutine exited (thereby holding the index value of a variable named MarkerNo)

    The variable is dimmed in a separate module as public.

    Code:
        Public Sub FindMarker()
            For MarkerNo As Integer = 0 To UBound(Markers)
                If A(MarkerNo) = B Then Exit Sub
            Next
            MarkerNo = -1 'Return -1 if not found
        End Sub

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,467

    Re: Variable not retaining value when exiting sub.

    if you have it declared public in a module:

    vb Code:
    1. Public Sub FindMarker()
    2.     For MarkerNo = 0 To UBound(Markers)
    3.         If A(MarkerNo) = B Then Exit Sub
    4.     Next
    5.     MarkerNo = -1 'Return -1 if not found
    6. End Sub

  3. #3
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: Variable not retaining value when exiting sub.

    Putting "As Integer" in your for loop declares it again for use within the loop ONLY

    Kris

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    816

    Re: Variable not retaining value when exiting sub.

    ohh of course..
    Thankyou.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width