• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 627
  • Last Modified:

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?
0
mcainc
Asked:
mcainc
  • 4
  • 2
1 Solution
 
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 TomlinsonMiddle School Assistant TeacherCommented:
"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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
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 TomlinsonMiddle School Assistant TeacherCommented:
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
 
mcaincAuthor Commented:
yeah, it looks like i can't

thanks for the help, its working smoothly so far!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now