I am passing an array of data extracted from a record in an .afu to extract specific data and the function called keeps crashing with the error: Parameter Name: Argument 'Array' is Nothing
and yet all the (simple) checks, see below, I can think off seems to suggest that the array is fully populated
'CHECK 1 if FxData is Nothing then theLW.WriteLine ("--- FxData array is nothing") else theLW.WriteLine ("--- FxData array is NOT nothing") END IF 'CHECK 2 - BRUTE FORCE. LIST SOME OF THE DATA For i = LBound(FxData) To 20 'UBound(FxData) theLW.WriteLine ("--- FxData " & FxData(i)) Next
extract of the code which works
Dim theXvalues() As Double Dim theYvalues() As Double Dim TData(),FxData() As Double 'EXTRACT data from the required record theSession.AfuManager.GetAfuData(pathAFUFile, RecordID, myAfuData)theYvalues = myAfuData.GetRealData(theXvalues) 'sise the arrays TData and FxData 'and transfer all the values using the function FnCopyArray() ReDim TData(UBound(theXvalues)) ReDim FxData(UBound(theYvalues)) TData=FnCopyArray(TheXvalues) FxData=FnCopyArray(TheYvalues) Function FnCopyArray(ByRef InputArray() As Double) As Double() 'The function copy all the value from the InputArray() to the array ArrayToPass () Dim i As Long Dim ArrayToPass() as Double ReDim ArrayToPass(UBound(InputArray)) For i = LBound(InputArray) To UBound(InputArray) ArrayToPass(i) = InputArray(i) Next I Return ArrayToPass End Function
the programm crashes when I called a 2nd function : GetMaxMin(FxData)
Function GetMaxMin(ByRef InputArray() As Double) As Double() Dim MaxValue, MinValue As Long Dim i, RankMax, RankMin As Long Dim DataToReturn(0 To 3) as Double For i = LBound(InputArray) To UBound(InputArray) 'do something Next i End Function
Could this be due to the fact that I gave the array FxData() a specific size (using the Redim)? The FnCopyArray() works fine! All the testing I did to develope the code worked fine
Any hints/suggestins/corrections are welcome
Solved! Go to Solution.
I don't see where you are populating theXvalues().
Why do you need a custom function to copy an array?
theXvalues is an output from (and seems to work!)
Dim myAfuData As CAE.AfuData
theSession.AfuManager.GetAfuData(pathAFUFile, RecordID, myAfuData)
theYvalues = myAfuData.GetRealData(theXvalues)
custom function to copy an array
I read/was told that one cannot equate array. I thought that may have been the source of the crash
While still playing with this issue I have found that the array FxData() may actually be overwritten as the programm does 3 loops. Will post something on that a bit later
Do you need to copy the array at all? Your GetMaxMin function appears to be only querying the values, not changing them...
Oh, and if you do need to copy the array, there is a .NET function for that:
" I read/was told that one cannot equate array."
Actually you can. This simple code:
Dim firstArray(9) As Double
For i = 0 To 9
firstArray(i) = i * 1.1
Dim copiedArray(firstArray.GetUpperBound(0)) As Double
copiedArray = firstArray
For i = 0 To 9
Provides this output:
If you do it that way, and then modify any of the elements in the first array, they are also modified in the copied array. To make a completely separate copy that will not have this limitation, use
copiedArray = firstArray.Clone()
instead. Then anything you do to the original is not reflected in the copy
thanks for that. Will look into into.
I think I was confusing myself and found that the followign may work
RecordID = LastRecordID+1 For i = 1 to 3 theLW.WriteLine("--Record ID is: " & RecordID.ToString) theSession.AfuManager.GetAfuData(pathAFUFile, RecordID, myAfuData) If i = 1 then FxData = myAfuData.GetRealData(TData) If i = 2 Then FyData = myAfuData.GetRealData(TData) If i = 3 Then FzData = myAfuData.GetRealData(TData) RecordID = RecordID+2 Next
In .NET, an array is a so-called "reference type". This means it's really just a pointer (a memory location).
So, suppose you write:
Dim copiedArray As Double() = originalArray
The originalArray thing is really the location in memory of the start of a sequence of doubles. The statement above sets copiedArray equal to this same location. So, now you have two variables pointing to this same location. Then, as Steve said, changing an element of originalArray will also change the corresponding element of copiedArray. This happens because these two elements are one and the same thing -- they are in the same location in memory. You haven't really copied the array at all, you have just invented a duplicate name for its location in memory.
If you really want a new and independent copy, use the Clone function.
Using Redim is not the recommended practice, these days. If you don't know in advance how many elements you'll be dealing with, it's usually better to use a List, instead of an array. You can keep adding elements to the List, and it will expand as needed. If you really want an array, you can get one from the List by using the ToArray function.