Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all

I am trying to write a shared method for custom math calculations. It takes reference to an array as input for the calculations. However, I need to be able to perform the calculations on only part of my array, and so I need to be able to pass that information to the method somehow. I have made a short example to explain what I mean, and you'll see the function call trying to indicate for the function to use the array dataArray's rows 2 through 3, in column 0.

Module Module1
    Sub Main()
        Dim dataArray(5,1) As Double
        'Create dummy data
        For i = 0 To 5
            dataArray(i,0) = i ^ 1.3
        Next
        'Perform calculation
        Dim b As Double = mySum(dataArray(2..3, 0))
    End Sub
End Module

Module myMath
    Function mySum(ByRef scalar() As Double) As Double
        Dim _sum As Double
        For i = 0 To scalar.Length - 1
            _sum = _sum + scalar(i)
        Next i
        Return _sum
    End Function
End Module


Also, is there a simple way for a function to return an array directly into an existing array? Pseudo code:

dataArray(2..3,1) = mySum(dataArray(2..3,0))


where

Function mySum(ByRef scalar() as double) as double()



As this type of calculations will be running for hours, I'm trying to avoid creating and passing new arrays for performance reasons.

Hope you have any tips on how to solve this.

Cheers
Posted
Updated 6-Sep-10 2:50am
v2

Why not pass the boundaries of the subset of the array that you are interested in.

Also, you are passing the array by ref, so I'm not sure what the issue is that you are worried about in terms of passing copies of the array.

VB
Module Module1

    Sub Main()
        Dim dataArray(5) As Double
        'Create dummy data
        For i = 0 To 5
            dataArray(i) = i ^ 1.3
        Next

        'Perform calculation
        Dim first As Integer = 2
        Dim last As Integer = 3
        Dim b As Double = mySum(dataArray, first, last)
    End Sub

    Function mySum(ByRef scalar() As Double, ByVal first As Integer, ByVal last As Integer) As Double
        Dim _sum As Double
        For i = first To last
            _sum = _sum + scalar(i)
        Next i
        Return _sum
    End Function

End Module



Hope that helps.

-Rd
 
Share this answer
 
Thank you, Richard.

I see this is the way Microsoft implemented methods in their array class too, passing the boundaries as arguments in the method.

Also, I had a quick peek on ArraySegment which gave me a passing hope for the type of functionality I wanted, but it's implementation was somewhat lacking.

Thanks again

-Fossie
 
Share this answer
 
Comments
Richard A. Dalton 7-Sep-10 10:27am    
Glad that helped.
For simplicity I changed the Multi-Dimensional Array in your question to a one dimensional array, but the principal would also work fine for multi-dimensional arrays too.

I was wondering after I posted whether you wanted to limit the called function so that it could ONLY access the specified portion of the Array.

The above approach relies on the code behaving itself and staying within first..last.

If you needed to enforce that restriction you might have to do a bit more work.

-Rd

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900