Return 2 values in a foreach

Asked

Viewed 252 times

0

I would like to return two values in one foreach, can help me?

    For Each item In Funcao()
        teste1 = gostaria de pegar aqui o resultado 1
        teste2 = gostaria de pegar aqui o resultado 2
    Next

Private Function Funcao() As String
    Dim resultado1 As String
    Dim resultado2 As String

    resultado1 = "Teste"
    resultado2 = "Teste2"

    Return resultado1

End Function
  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

2 answers

1

0

Return two literal values in a single variable is impossible, but you can return one List<string> with two values and so use them in your block For Each, enumerating the return of Funcao():

Private Function Funcao() As String() ' Array enumerável
    Dim Retorno As New List(Of String)
    Dim resultado1 As String
    Dim resultado2 As String

    resultado1 = "Teste"
    resultado2 = "Teste2"

    Retorno.Add(resultado1)
    Retorno.Add(resultado2)

    ' Retorna a lista com os elementos resultado1 e resultado2
    Return Retorno.ToArray()
End Function

And to use:

Dim Resultados As String() = Funcao()
teste1 = Resultados(0) 'Teste'
teste2 = Resultados(1) 'Teste2'

If you want to list:

For Each item In Resultados
     teste1 = item(0) 'Teste'
     teste2 = item(1) 'Teste2'
Next

Browser other questions tagged

You are not signed in. Login or sign up in order to post.