RETORNAR UM VETOR COMO PARAMETRO?
como se retorna um vetor como parametro outra ocisa eu queria tb imprimir um vetor começando do fim
Prontim... está testado.... e se resolver.. fecha o topico...
Private Sub Command1_Click()
Combo1.Clear
Dim vetor(50)
Randomize
For i = 1 To 50
vetor(i) = Int(Rnd(50) * 100)
Next
'essa parte alimenta o vetor...
'para saber quais os valores... coloque um combo
For i = 1 To 50
Combo1.AddItem "O valor do vetor na posição " & i & " é :" & vetor(i)
Next
End Sub
o problema é assim por exemplo eu alimentaria o vetor dentro de um command buton dai eu chamaria a função dai eu passava o vetor como parametro para a função dai a função ia fazer uns calculos e retorna-se o vetor dentro do comand
Em uma sub o tipo de um vetor é "Variant", ou seja, suponha que você tenha criado uma sub "ImprimeVetor" que imprima o vetor fornecido como parametro, do fim ao inicio.
Exemplo
[txt-color=#000080]Sub ImprimeVetor(vetor as Variante, numElementos as Integer)
Dim i as Integer
For i = numElementos-1 to 0 step -1
list1.AddItem vetor(i)
Next
End Sub[/txt-color]
Onde:
vetor é o vetor de elementos que vc forneceu como parametro
numElementos é o numero de elementos que o vetor contém
list1 é uma ListBox onde serão imprimidos os elementos do ultimo até o primeiro
Para invocar a sub dou um exemplo abaixo:
[txt-color=#000080]Private Sub Command1_Click()
Dim vetor(5) as String
vetor(0) = "AAA"
vetor(1) = "BBB"
vetor(2) = "CCC"
vetor(3) = "DDD"
vetor(4) = "EEE"
ImprimeVetor vetor, 5
End Sub[/txt-color]
O resultado é:
A listBox "list1" tera os seguintes valores
EEE
DDD
CCC
BBB
AAA
Caso tenha tirado sua dúvida, encerre o tópico
Ah, e se for o caso de uma função que retorne um valor, o tipo é o mesmo
dai seria
Function AlgumaCoisa(vetor as Variante) As Variante
'Aqui ficam os calculos do "vetor" como indicado acima
End Function
na hora de chamar a função é só fazer
vetor = AlgumaCoisa(vetor)
dai seria
Function AlgumaCoisa(vetor as Variante) As Variante
'Aqui ficam os calculos do "vetor" como indicado acima
End Function
na hora de chamar a função é só fazer
vetor = AlgumaCoisa(vetor)
entendo.. vc tera q declarar o vetor num módulo.. para ser global... deve ser isso agora.. hehehe
se resolver.. fecha o tópico... e me pontua ae!
'em um modulo
Public vetor(50)
'Ja normalmente num formulario
Private Sub Command1_click()
vetor(10) = 100
vetor(10) = calcula(vetor(10))
MsgBox vetor(10)
End Sub
Private Function calcula(valor)
calcula = valor + Day(Now)
calcula = calcula * Month(Now)
calcula = calcula * Year(Now)
'calculos
End Function
se resolver.. fecha o tópico... e me pontua ae!
SIBEM:
o HAILTON.FERRAZ ta mais proximo de responder a minha pergunta
HAILTON.FERRAZ:
na hora de chamar a função é só fazer
vetorA = AlgumaCoisa(vetorB)
o valor do 1 indice do vetorA vai ficar com o valor do vetorB do 1 indice?Na hora que vetor A recebe o vetorB eles se relacionan os indices
o HAILTON.FERRAZ ta mais proximo de responder a minha pergunta

HAILTON.FERRAZ:

na hora de chamar a função é só fazer
vetorA = AlgumaCoisa(vetorB)
o valor do 1 indice do vetorA vai ficar com o valor do vetorB do 1 indice?Na hora que vetor A recebe o vetorB eles se relacionan os indices
bem... ai vai um exemplo "meia boca" de como usar array tanto como envio quanto recebimento de informações a uma função.
dim aVetor1() as Variant
dim aVetor2(5) as Variant
aVetor2(1) = 1
aVetor2(2) = 2
aVetor2(3) = 3
aVetor2(4) = 4
aVetor2(5) = 5
aVetor = RetornaArray(aVetor2)
Function RetornaArray(paramArray as Variant) As Variant
Dim xx As Integer
Dim yy As Integer
Dim aTemp(UBound(paramArray))
yy = 1
For xx = UBound(paramArray) to 1 Step -1
aTemp(yy) = paramArray(xx)
yy = yy + 1
Next xx
RetornaArray = aTemp
End Function
dim aVetor1() as Variant
dim aVetor2(5) as Variant
aVetor2(1) = 1
aVetor2(2) = 2
aVetor2(3) = 3
aVetor2(4) = 4
aVetor2(5) = 5
aVetor = RetornaArray(aVetor2)
Function RetornaArray(paramArray as Variant) As Variant
Dim xx As Integer
Dim yy As Integer
Dim aTemp(UBound(paramArray))
yy = 1
For xx = UBound(paramArray) to 1 Step -1
aTemp(yy) = paramArray(xx)
yy = yy + 1
Next xx
RetornaArray = aTemp
End Function
Bom , Tenta assim
Private Sub cmd_Calcula_Vetor_Click()
dim var_cRet() as string
dim var_i as integer
var_cRet= fc_Calcula_Vetor(valor1,valor2,valor3)
for var_i=lbound(var_cRet) to ubound(var_cRet)
msgbox var_cRet(var_i)
next
'c quizer ler do fim
for var_i=Ubound(var_cRet) to lbound(var_cRet) step -1
msgbox var_cRet(var_i)
next
End Sub
Public fc_Calcula_Vetor(paramarray par_cValores() as variante)
dim var_i as integer
for var_i=lbound(par_cValores) to ubound(par_cValores)
par_cValores=par_cValores* x + y
next
fc_Calcula_Vetor=par_cValores
end function
espero ter ajudado
Private Sub cmd_Calcula_Vetor_Click()
dim var_cRet() as string
dim var_i as integer
var_cRet= fc_Calcula_Vetor(valor1,valor2,valor3)
for var_i=lbound(var_cRet) to ubound(var_cRet)
msgbox var_cRet(var_i)
next
'c quizer ler do fim
for var_i=Ubound(var_cRet) to lbound(var_cRet) step -1
msgbox var_cRet(var_i)
next
End Sub
Public fc_Calcula_Vetor(paramarray par_cValores() as variante)
dim var_i as integer
for var_i=lbound(par_cValores) to ubound(par_cValores)
par_cValores=par_cValores* x + y
next
fc_Calcula_Vetor=par_cValores
end function
espero ter ajudado
Citação:FABIO ALEXANDRE Public fc_Calcula_Vetor(paramarray par_cValores() as variante)
dim var_i as integer
for var_i=lbound(par_cValores) to ubound(par_cValores)
par_cValores=par_cValores* x + y
next
fc_Calcula_Vetor=par_cValores
end function
se der erro Muda fc_Calcula_Vetor=par_cValores para fc_Calcula_Vetor=par_cValores()
Para receber um array como parà ¢metro eu costumo fazer assim :
o tipo pode ser qualquer um , inclusive string.
Para retornar um array seria :
Mais sobre o assunto em : http://www.vbexplorer.com/VBExplorer/vb_feature/oct2000/passing_array_to_function.asp
Private Sub RecebeArray(intArray() As Integer)o tipo pode ser qualquer um , inclusive string.
Para retornar um array seria :
Private Function RetornaArray() As Variant
Dim x(3) As Integer 'declara o array
x(0) = 1
x(1) = 2
x(2) = 3
x(3) = 4
ReturnaArray = x 'retorna o array representado por x
End FunctionMais sobre o assunto em : http://www.vbexplorer.com/VBExplorer/vb_feature/oct2000/passing_array_to_function.asp
Tópico encerrado , respostas não são mais permitidas