-
August 13th, 2005, 06:17 AM
#1
Can an array be passed to a Function as parameter..........
I am using the following code
Please tell me whats wrong here in.
nd any alternative for it.
Basically i want to get max no of an array through a function.
nd i m passing an array to the function "Max".
it gives me error "Subscript out of range"
can i pass an array as Argument(Parameter) to a function.
If yes then How?
Code:
Dim ar() As Integer
Private Sub Command1_Click()
Dim i As Integer ''Holds array length
i = InputBox("Enter Total elements")
For j = 0 To i
ReDim Preserve ar(j)
ar(j) = InputBox("Enter the Nos to GetMax Value Of")
Next
MsgBox max(ar())
End Sub
Public Function max(ParamArray a()) As Integer
Dim temp As Integer
For b = 0 To UBound(a())
If a(b) > a(b + 1) Then <==Error Here
temp = a(b)
Else
temp = a(b + 1)
End If
Next
max = temp
End Function
Thanks in Adv.
-
August 13th, 2005, 08:07 AM
#2
Re: Can an array be passed to a Function as parameter..........
A ParamArray is used to define a unknown amount of parameters, not to pass a parameter.
The declaration of the function should be this:
Code:
Public Function max(a() as Integer) As Integer
-
August 13th, 2005, 07:07 PM
#3
Re: Can an array be passed to a Function as parameter..........
But, note that the type of the array does have to be defined.
Mike
-
August 14th, 2005, 09:33 AM
#4
Re: Can an array be passed to a Function as parameter..........
in the function you have defined, how can the function access an element that has an index greater than that of the array.
for example, yuo have an array of size 9(0-8). now b is going to be 0 to 8, so no problem abt a(b), but wht abt a(b+1), it will finally be a(9),got it.
try to use the upper limit of the for loop in the function as -
ubound(a())-1
another way is to define the array as variant data type, but you have specified that you are going to mreturn the max. value in the array to the user, obviously the array won't contain struings, it may contain integers or floats or in the best case longs.
i hope that helps...
reply.
-
August 18th, 2005, 11:15 PM
#5
Re: Can an array be passed to a Function as parameter..........
Thanks for ur comments.
ofcourse the problem is same as u told but it always takes UBound=0.
nd when a(b+1) executes it generates error coz it becomes a(1).
The code is now working with some changes
Code:
Dim ar() As Integer
Private Sub Command1_Click()
Dim i As Integer ''Holds array length
i = InputBox("Enter Total elements")
For j = 0 To i
ReDim Preserve ar(j)
ar(j) = InputBox("Enter the Nos to GetMax Value Of")
Next
MsgBox max(ar())
End Sub
Public Function max(a() As Integer) As Integer
Dim temp As Integer
temp = a(0)
For b = 1 To UBound(a())
If temp < a(b) Then
temp = a(b)
End If
Next
max = temp
End Function
This was nice to see such usefull information.
With lots of apreciation nd thanks.
Take Care all of u.
Silly Star
Originally Posted by pangolin_10
in the function you have defined, how can the function access an element that has an index greater than that of the array.
for example, yuo have an array of size 9(0-8). now b is going to be 0 to 8, so no problem abt a(b), but wht abt a(b+1), it will finally be a(9),got it.
try to use the upper limit of the for loop in the function as -
ubound(a())-1
another way is to define the array as variant data type, but you have specified that you are going to mreturn the max. value in the array to the user, obviously the array won't contain struings, it may contain integers or floats or in the best case longs.
i hope that helps...
reply.
Last edited by Silly Star; August 18th, 2005 at 11:30 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|