0
Dim someNumber As Variant
Dim myIntegerNumber as Integer
Set someNumber = GetTheNumber("Seven") ' Fails

If someNumber = Nothing Then ... End If
If someNumber = 5 Then myIntegerNumber = someNumber

If GetTheNumber returns Nothing this works fine. If it returns 7 then the code falls over for trying to use Set with a number.

Likewise, if I invert it so it just does someNumber = GetTheNumber("Seven") then it will work for 7 but fail for Nothing.

How can I solve this problem or is there a better way of having my function indicate the operation couldn't be completed?

NibblyPig
  • 51,118
  • 72
  • 200
  • 356
  • 1
    There is an [answer](https://stackoverflow.com/q/35750449/11683) to that, but [actually](https://meta.stackexchange.com/q/66377/147640) you want to not use `Nothing` to indicate the absence of an integer. Use `Empty` or `Null`. – GSerg Nov 04 '21 at 11:56
  • IsObject, thanks. Note this isn't an XY question as I state the problem and my solution, not just my solution. – NibblyPig Nov 04 '21 at 11:57
  • 2
    No, IsObject is not the answer. The answer is to pass it to a sub, because only then the assignment will work correctly regardless of whether it's an object or not. Otherwise you won't even get to the point where you can call IsObject. In any case the real answer is to not do that at all, rather than to fix it, hence the XY question. – GSerg Nov 04 '21 at 12:00
  • The simplest way to deal with the let/set conundrum is to use an intermediate variable which would be a collection. The Add method of a collection is agnostic to the type of variable thatt is added. Thus you can capture the result of getthenumber by using 'tmpColl.add getthenumber("Seven") and then do whatever tests are necessary on .Item(0) of tmpColl. There are other better solution available. – freeflow Nov 04 '21 at 14:08
  • I think it depends on your use case, the duplicate question linked has several answers. I used `IsObject` initially but changed it to a function that returns true/false and takes a byref variable for the output, which I think is nicer than using a collection but just has more overhead – NibblyPig Nov 05 '21 at 11:17

0 Answers0