VB.NET best way to check if index is out of bounds in array

Is there an easy way to check if an index is out of bounds when using arrays so that I can avoid the first chance exception?
mcaincAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mcaincAuthor Commented:
I should clarify, I'm having a strange issue with this piece of a function that uses an array of a custom structure:

For x = 1 To UBound(PrivateMessageQ)
  PMQt = PrivateMessageQ(x)
  ...
Next

I'm getting the first chance exception in the line: PMQt = PrivateMessageQ(x)

This function is in a shared module & is being called by other threads... I believe that something is happening simultaneously & changing the array size before the loop has completed.

Does this sound right? Is there a way to lock functions like this until they are completed so I can avoid this?
0
mcaincAuthor Commented:
Oops, the function is in a Class that is used multiple times however the "PrivateMessageQ" array is a public array...

0
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
"Is there a way to lock functions like this until they are completed so I can avoid this?"

Use SyncLock...you need an Object that all threads can access:

    Public Class Sync
        Public Shared SyncObject As New Object
    End Class

Then you utitlize it like this:

    SyncLock Sync.SyncObject
        For x = 1 To UBound(PrivateMessageQ)
            PMQt = PrivateMessageQ(x)
            ...
        Next
    End SyncLock

0
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

mcaincAuthor Commented:
ahhhh ok that makes sense, what about if i have some type of Try Catch exception or Exit Sub / Exit Function in the code...

Do I need to call End SyncLock in this event as well?

Example:

Public Sub Something()
    SyncLock Sync.SyncObject

    Try
            For x = 1 To UBound(PrivateMessageQ)
                PMQt = PrivateMessageQ(x)
                ...
            Next
    Catch
           Debug.Writeline("error")
           End SyncLock
    End Try

    End SyncLock
End Sub

or

Public Sub Something()
    SyncLock Sync.SyncObject

            For x = 1 To UBound(PrivateMessageQ)
                PMQt = PrivateMessageQ(x)

                If a = b Then
                    End SyncLock
                    Exit Sub
                End If
            Next

    End SyncLock
End Sub

0
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Hmm...never explicitly tested it but it shouldn't automatically "release" the lock once the code block has exited...shouldn't matter how the block was left.   Don't think you can put multiple "End SyncLock" statements in there anyhoo.  =)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mcaincAuthor Commented:
yeah, it looks like i can't

thanks for the help, its working smoothly so far!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.