|
-
December 19th, 2007, 03:37 AM
#1
pass array
this function (it's not mine) crashes when array size is very large (> 32768).
Public Function GetArrayMinxMax(arrintArray() As Integer,
intArrayUpperBound As Long) As String
Dim intMin As Integer
Dim intMax As Integer
Dim intLoop As Integer
intMin = 5000
intMax = 0
For intLoop = 0 To intArrayUpperBound
If arrintArray(intLoop) > intMax Then intMax = arrintArray(intLoop)
If arrintArray(intLoop) < intMin Then intMin = arrintArray(intLoop)
Next intLoop
End Function
-
December 19th, 2007, 04:05 AM
#2
Re: pass array
32768 is the maximum size for an Integer. Why not make the arrintArray() parameter in your GetArrayMinxMax function, a long. Like this :
Code:
Public Function GetArrayMinxMax(arrintArray() As Long,
intArrayUpperBound As Long) As String
That's all I can advise based on what you gave.
And pretty please with sugar on top, use [CODE] [/CODE] Tags when posting code.
-
December 19th, 2007, 07:53 PM
#3
Re: pass array
 Originally Posted by HanneSThEGreaT
32768 is the maximum size for an Integer. Why not make the arrintArray() parameter in your GetArrayMinxMax function, a long. Like this :
Code:
Public Function GetArrayMinxMax(arrintArray() As Long,
intArrayUpperBound As Long) As String
That's all I can advise based on what you gave.
And pretty please with sugar on top, use Tags when posting code. 
Right idea, but it looks like the function attempts to determine the maximum and minimum values in the array. As far as I can tell, the data type of the array shouldn't be relevant -- it's the data type of the loop counter that needs to change.
I'm also not quite sure what the function is for, as it doesn't return a value or alter a ByRef passed variable. I'd do something like this:
Code:
Public Sub GetArrayMinxMax(iArray() As Integer, ByRef iMax As Integer, ByRef iMin As Integer)
Dim lCount As Long
iMax = 0 'Initialize the maximum.
For lCount = LBound(iArray) To UBound(iArray) 'Loop through the array.
If iArray(lCount) > iMax Then iMax = iArray(lCount) 'Check against cached max.
Next lCount
iMin = iMax 'The min has to be lower.
For lCount = LBound(iArray) To UBound(iArray) 'Second pass.
If iArray(lCount) < iMin Then iMin = iArray(lCount) 'Check against cached min.
Next lCount
End Sub
Note that this also prevents errors if called with a non-zero based array and eliminates the need to pass a bounding variable. In this case 2 passes through the array also ensures correct returns if the minimum value is higher than 5000. Also, passing the return variables ByRef lets you return both the minimum and maximum without using string handling functions. If you want it to work with arbitrarily signed numbers, set iMax to -32768 instead of 0.
Example calling code:
Code:
Private Sub CallingIE()
Dim iTest() As Integer, iMax As Integer, iMin As Integer
ReDim iTest(100) 'Build a test array.
For iMax = 0 To 100
iTest(iMax) = Rnd * 10000
Next iMax
Call GetArrayMinxMax(iTest, iMax, iMin) 'Call the function.
Debug.Print iMax & vbTab & iMin
End Sub
-
December 19th, 2007, 08:12 PM
#4
Re: pass array
One loop is good, but you NEED to add RANDOMIZE. I like that, passing the return values BYREF
Code:
Option Explicit
Private Sub Form_Load()
Dim iTest() As Integer, iMax As Integer, iMin As Integer
Randomize
ReDim iTest(100) 'Build a test array.
For iMax = 0 To 100
iTest(iMax) = Rnd * 10000
Next iMax
Call GetArrayMinxMax(iTest, iMax, iMin) 'Call the function.
Debug.Print iMax & vbTab & iMin
End Sub
Public Sub GetArrayMinxMax(iArray() As Integer, ByRef iMax As Integer, ByRef iMin As Integer)
Dim lCount As Long
iMax = 0 'Initialize the maximum.
iMin = 10000 'The min has to be lower.
For lCount = LBound(iArray) To UBound(iArray) 'Loop through the array.
If iArray(lCount) < iMin Then iMin = iArray(lCount) 'Check against cached min.
If iArray(lCount) > iMax Then iMax = iArray(lCount) 'Check against cached max.
Next lCount
End Sub
Last edited by dglienna; December 19th, 2007 at 08:15 PM.
-
December 20th, 2007, 02:04 AM
#5
Re: pass array
 Originally Posted by Comintern
Right idea, but it looks like the function attempts to determine the maximum and minimum values in the array. As far as I can tell, the data type of the array shouldn't be relevant -- it's the data type of the loop counter that needs to change.
Good catch! 
Yeah, I just saw the 32768 ( which is actually 32767 BTW ) and Integer. that's where I stopped reading the post
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
|