Problem with return value of array

hello group, I need your help.
I have a function from which I would like to obtain 2 values, stored in an array. Is it possible to return these two values? How can I recall the value positioned in box 0 of the array and that of box 1?

Function DELETE(Table as string, Name as string, MyData as string) as integer
 Dim MyArray(2) as integer
         MyArray(0)=111
         MyArray(1)=222
return MyArray

in the body …

messagebox(" My Array returned " + DELETE("TEST1","TEST2","TEST3").ToString +" value") ’ <<< ???

Hi,

MyArray(0).toString?

MyArray is the return value of DELETE.
how do i call this value in the program body, i think i have to set an index somewhere, but i dont understand how to do it.

method need to return an array, i not see this in your example

grafik

Public Function Untitled() As integer()
  Var ar() As Integer
  ar.Add(100)
  ar.Add(200)
  Return ar
  
End Function

calling this method

Var ar() As Integer

ar = Untitled()

System.DebugLog ar(0).ToString

if you need to delete values in database it is where id in(1,2,3,4)

If ar() have other parameteres for example Var ar(i as integer, d as string) as integer
how i calling this method ? System.DebugLog ar((1,“HI”),0).tostring ?

You would store an object, as example a class with id and name property?

Var xy() as myclass

Or mixed content in a one dimensional array would be variant not integer.

1 Like

I think, you should use a dictionary instead of an array.

and what would it be? I’m interested in passing two values ​​as the result of a function

too difficult for me, I’m not an expert in programming.

Look into the ByRef keyword, that is one way to accomplish this.

Then all you need to do is change the return type to an array of integers as Markus mentioned in post #4

Everything else you’ve been told is not an answer to your question.

You can try return Array(1,2) but later you need access by index 0 and 1

A class object is often better if you need this kind of return frequently.
You can return this direct or as array / list.

Var o as new ClassReturn(1,“Text“)
In this case/example a constructor method stores arguments in the class properties.
To access properties is easy
o.Id
o.Description

Public Function getTwoValues() As dictionary
  Dim returnDictionary As New dictionary
  
  returnDictionary.Value("FirstValue") = "Some information"
  returnDictionary.Value(1234) = "Some other information"
  
  return returnDictionary
End Function

and then you get the dictionary as a return:

Dim myValues As dictionary = getTwoValues

And use the valuies set inside the method:

MessageBox(myValues.Value("FirstValue"))
MessageBox(myValues.Value(1234))

I was trying to be nice before by not calling your post out individually, but this is such a bad idea. Using dictionaries and variants for lack of familiarity with the framework will absolutely lead to problems in the future.

For the second time

@Federico_Giannetti, all you need to do is change the return type from Integer to Integer() and you are on your way.

3 Likes

Thanks everyone for the suggestions, I will study the solution.

1 Like

ByRef’ing his return values is also valid, and in my opinion superior, if the function is always going to return exactly two values and those values have specific contextual meanings (i.e., they’re not just anonymous elements in an array, they are more specific, like ItemFlavor and ItemColor). The functionality of the code is much more obvious to the reader.

I disagree with this wholeheartedly. ByRefs make code that is less self documenting. We can disagree on this, I am okay with that.

What irks me is that OP had only asked for how to return an array. Why would anyone delve into byrefs, classes, or dictionaries? It was a simple question with a simple answer.

6 Likes

Really? If you have something like this:

Function FindItem(Input as someClass(), lookingFor as string, ByRef Name as String, ByRef Location as String) as Boolean

…you get calling code that looks like this:

Dim FoundName as String
Dim FoundLocation as String

If FindItem(theInput, "Pineapple", FoundName, FoundLocation) Then
     ProcessItem(FoundName, FoundLocation)
End

…plus you get AutoComplete that specifies exactly what each variable will return.

If you do the array return, you have:

Function Function FindItem(Input as someClass(), lookingFor as string) as String()

The calling code has an ambiguous return value, could be nil or an empty array; plus you have to remember (or create constants for) the item indices:

Dim FoundItems() as String

FoundItems=FindItem(theInput, "Pineapple)

If FoundItems<>nil and Ubound(FoundItems)=1 Then
    ProcessItems(FoundItems(0), FoundItems(1))
End

AutoComplete doesn’t give you any contextual understanding of what the return value is going to be, just a string array.

It’s a matter of style, of course, because both approaches will work given they are properly implemented and utilized. I prefer the ByRef approach because it produces code that is more explicit about return values, and it provides a non-overloaded indication of whether the function call was successful.

Perhaps we have different notions of the problem.

If the exact number of items you are going to return is 2, then why not use a Pair:

Function DELETE(Table as string, Name as string, MyData as string) as Pair
 Dim p as Pair
         p = 111:222
return p
1 Like

Then have the technical debt of being stuck using a Pair if you ever needed to return more elements in the future. This is another design I would not recommend.

2 Likes