ExtensionMethod.NET Home of 820 C#, Visual Basic, F# and Javascript extension methods

ArrayNullConditional

Checks the item in the array. If the index passed in is equal to size or larger than array NOTHING is returned. Otherwise returns STRING from the array. Could easily be extended to other types/objects, just change return type on extension from string to your needs.

Source

Public Module Extension
    <System.Runtime.CompilerServices.Extension> _
    Public Function Extend(arr As Array, index as INteger) As String
        if (arr.Length <= index) then
            return Nothing 'Could return ""
        Else
            Dim retValue as string
            if (arr(index) is nothing) then
                return Nothing 'Could return ""
            Else
                return arr(index)
            End If
        End if
    End Function
End Module

Example

Dim strArr() as string = {"Test1","Test2", Nothing}
dim NullStr = strArr.Extend(3)
dim NothingSTr = strArr.Extend(2)
dim PopStr = strArr.Extend(0)
console.WriteLine(NullStr)
console.WriteLine(NothingSTr)
console.Writeline(PopStr)

Author: RJ Kelly

Submitted on: 3 feb 2017

Language: VB

Type: Array

Views: 1451