I am creating an add-in for a piece of software our company uses, so I'm working pretty extensively with that program's API. There are many COM object members which return or contain an array that I need to work with. Sometimes functions will return an uninitialized array (Null or "Nothing" return) or sometimes they may return an initialized array that is not allocated. My goal is to eliminate redundant code in the form of If
/Else
blocks before using each array, or Try
/Catch
blocks to handle exceptions thrown by the array being un-initialized or un-allocated.
Note: I'm not trying to validate any data here - that can be done in the calling code or in a function or whatever - I'm simply looking to determine if an array is in a usable state (e.g. initialized and allocated with at least one member)
I've created a member extension for arrays to check if the array is both initialized and allocated.
I'd like to get some input on whether or not this is a well designed solution or if it's total bupkis, if I'm missing anything else I should be checking for, and if improvements can be made in terms of robustness, efficiency, or general best practices; I would like to make it as fast as possible for dealing with large arrays.
Imports System.Runtime.CompilerServices
Module Module1
#Region "Extension Methods"
<Extension()>
Public Function IsAllocated(ByVal inArray As System.Array) As Boolean
Dim FlagEx As Boolean = True
Try
If inArray Is Nothing Then
FlagEx = False
ElseIf inArray.Length <= 0 Then
FlagEx = False
ElseIf inArray(0) Is Nothing Then
FlagEx = False
End If
Catch ex As Exception
FlagEx = False
End Try
Return FlagEx
End Function
#End Region
End Module