Get an empty array

BTW: As so often, the MBS Plugins have a solution for this :wink:

If tmpObjectNames = Nil Then tmpObjectNames = ArrayStringMBS()

This was what I was looking for! Great!
Many thanks to you and @Christian_Schmitz :wink:

1 Like

I don’t get why you need such more than this?

Dim tmpObjectNames() As String = RaiseEvent getObjects
If tmpObjectNames = Nil Then 
  Var strs(-1) As String
  tmpObjectNames = strs
End If

The variant suggestion was to make sure you’d check nil or reassign to an array but since this would work the same it’s probaly much easier to do so or use the MBS function with method overhead.

I don’t need more. But that’s just (in my eyes) bad code… And I don’t want bad or, let’s say, ugly code in my code. :wink:

So you can put the ugly code in a method and call it.

Function GetNewStringArray() as String()
   var a() as string
   return a
End Function

Dim tmpObjectNames() as String = RaiseEvent getObjects
if tmpObjectNames = Nil then tmpObjectNames = GetNewStringArray()

It’s exactly the same as MBS (and probably identical code). If you have MBS, the by all means use it. If you don’t have MBS, then you don’t need a license just for this. That said, MBS has a ton of useful functions.

5 Likes

Also, in my opinion Xojo arrays are “weird” - see a long discussion of some of the quirks here: Xojo WTF

// Simpler take on that
// Just using good naming makes the code perfect, and maybe faster than alternatives
Var tmpObjectNames() as String = RaiseEvent getObjects
if tmpObjectNames = Nil then
   Var noObjects() as String
   tmpObjectNames = noObjects
End

And now we’ve gone round in circles exactly once. It was precisely this solution that I described in the initial post as “I want to avoid that” :wink:

1 Like

Ah, ok. Weird to avoid that just to prefer calling a function instead, but preferences are preferences… :man_shrugging:t2:, then Tim’s take is really what you want. Or…

Function GetObjectNames as String()
  Var objectNames() as String = getObjects()
  If objectNames = Nil 
     Var noObjects() as String
     Return noObjects
  End
  Return objectNames
End Function

// Or you can go deeper and pack the entire desired functionality in a method
Dim tmpObjectNames() as String = GetObjectNames()

:smile:

Xojo just needs macros. but that’s for another thread…:wink:

Not for this case.