+ Reply to Thread
Results 1 to 4 of 4

Proper way to return empty object

Hybrid View

fatkatie Proper way to return empty... 01-30-2020, 09:10 AM
Daishiknyte Re: Proper way to return... 01-30-2020, 02:22 PM
mjr veverka Re: Proper way to return... 01-30-2020, 02:44 PM
LJMetzger Re: Proper way to return... 02-22-2020, 03:31 PM
  1. #1
    Registered User
    Join Date
    01-13-2020
    Location
    usa
    MS-Off Ver
    office 365
    Posts
    15

    Proper way to return empty object

    I have a function that returns an object. The object is always instantiated but could be empty (Dictionary). If it's empty I want to return something to the IF construct that says false.

    What is the proper way of recognizing a "failure" with functions that return objects?

    Thank you

  2. #2
    Registered User
    Join Date
    11-06-2012
    Location
    Texas
    MS-Off Ver
    O365
    Posts
    27

    Re: Proper way to return empty object

    If you know the dictionary exists and need to see if there's anything in it, check if .Count returns 0.

  3. #3
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,273

    Re: Proper way to return empty object

    Quote Originally Posted by fatkatie View Post
    ... to return something to the IF construct that says false ...
    There are a few methods that will verify / set it up, you have one of them above, others are e.g.: IsArray (arr = dict.Keys => IsArray(arr)), IsEmpty(arr = dict.Keys => IsEmpty(arr)), etc., but what does your code look like ?

  4. #4
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Proper way to return empty object

    The following working example code may help you.
    Sub TestObjects()
    
      Dim myDictionary As Object
      Dim myRange As Range
      
      Dim iCount As Long
      
      
      If myDictionary Is Nothing Then
        Debug.Print "There is NO Dictionary Object"   'Output to Immediate Window - Ctrl G in the Debugger
      End If
    
      'Create the Dictionary Object
      'Reference: http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/A_3391-Using-the-Dictionary-Class-in-VBA.html
      'Reference: http://www.snb-vba.eu/VBA_Dictionary_en.html
      'KEY:  Item Name
      'ITEM: Count
      Set myDictionary = CreateObject("Scripting.Dictionary")
      'myDictionary.CompareMode = vbBinaryCompare 'case sensitive
      myDictionary.CompareMode = vbTextCompare 'case insensitive
    
    
      If Not myDictionary Is Nothing Then
        Debug.Print "There is a Dictionary Object"
      End If
    
      iCount = myDictionary.Count
      Debug.Print "The Dictionary contains " & iCount & " items."
     
      'Add values to the Dictionary
      myDictionary.Add "abc", 1
      myDictionary.Add "def", 888
    
      iCount = myDictionary.Count
      Debug.Print "The Dictionary contains " & iCount & " items."
     
      Set myRange = Intersect(Range("A1:A12"), Rows(1))
      If myRange Is Nothing Then
        Debug.Print "Intersect(Range(""A1:A12""), Rows(1)) is Nothing."
      Else
        Debug.Print "Intersect(Range(""A1:A12""), Rows(1)) contains the range " & myRange.Address(False, False) '(False, False) removes '$' from Address
      End If
     
      Set myRange = Intersect(Range("A1:A12"), Rows(22))
      If Not myRange Is Nothing Then
        Debug.Print "Intersect(Range(""A1:A12""), Rows(22)) contains the range " & myRange.Address(False, False)
      Else
        Debug.Print "Intersect(Range(""A1:A12""), Rows(22)) is Nothing."
      End If
     
     
    
      'Clear Object Pointers
      Set myDictionary = Nothing
      Set myRange = Nothing
    
    End Sub
    Lewis

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Formula to return empty string instead of null when column table is empty
    By jaryszek in forum Excel Formulas & Functions
    Replies: 26
    Last Post: 02-14-2020, 06:37 AM
  2. Return proper value from chart
    By kmlloyd in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-21-2016, 02:56 PM
  3. value return is not on proper header
    By darksquare in forum Excel General
    Replies: 3
    Last Post: 04-09-2016, 02:10 AM
  4. Find First proper empty row
    By Rob K in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-09-2015, 08:09 AM
  5. [SOLVED] Proper way to return to beginning of sub?
    By smaier69 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-14-2013, 08:44 PM
  6. Function to return object reference given object name
    By 6StringJazzer in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-28-2010, 09:07 PM
  7. Replies: 2
    Last Post: 03-31-2009, 03:55 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1