CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: pass array

  1. #1
    Join Date
    Nov 2007
    Posts
    15

    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

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    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 &#091;CODE] &#091;/CODE] Tags when posting code.

  3. #3
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    516

    Re: pass array

    Quote 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

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    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.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: pass array

    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured