Create zero-elements arrays

The .NET framework lets you create two types of “empty” arrays: unitialized arrays and arrays that are initialized with zero elements. Uninitialized arrays are actually array variables that are set to Nothing, whereas zero-element arrays are non-Nothing variables that point to arrays with zero elements. Here is the (undocumented) method for creating zero-element arrays:

Dim arr(-1) As Integer   ' or whatever type you need

If you have a routine that returns an array, you can decide whether you want to return Nothing or a zero-element array when an empty array should be returned. In general, returning a zero-element array makes for a more linear code in the caller. Consider this routine, that returns all the items in a string array that contain a given substring:

Function Matches(ByVal arr() As String, ByVal Search As String) As String()    Dim al As New ArrayList()    Dim s As String    For Each s In arr        If s.IndexOf(Search) >= 0 Then al.Add    Next    ' return Nothing if no matches    If al.Count Then Return Nothing    ' else move the elements into a string array    Dim res(al.Count - 1) As String    Dim i As Integer    For i = 0 To al.Count - 1        res(i) = al(i)    Next    ' return the array    Return resEnd Function

The caller of the above routine must discern the Nothing case from regular case:

Dim res() As String = Matches( arr, "Find this")If res Is Nothing Then    Console.WriteLine("Found 0 matches")Else    Console.WriteLine("Found {0} matches", res.Length)End If

Now, consider what happens if you delete these two lines in the Matches routine:

    ' return Nothing if no matches    If al.Count Then Return Nothing

Not only is the Matches routine simpler, also the caller requires less code, because it doesn’t have to check for Nothing first:

Dim res() As String = Matches( arr, "Find this")Console.WriteLine("Found {0} matches", res.Length)

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.