[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 617
  • 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

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