excelvbaparameter-passingcustom-type

Using a class object as a parameter within the same class object


I'm having trouble with a custom class module called "Spectrum"

The following line of code should subtract spectrum "B" from spectrum "A" directly modifying the private variables within "A".

    A.subtract(B)

But at run-time, I get a "Run-time error '438': Object doesn't support this property or method"

Here is the subroutine from the "Spectrum" custom class module:

    Private pYValues(1 TO 10000) as Double
    Private pIndex as Integer

    Public Sub Subtract (Value as Spectrum)
        Dim i as Integer
        For i = 1 to pIndex
            pYValues(i) = pYValues - Value.YValues(i)
        Next i
    End Sub

    Public Property Get YValues(index as Integer)
        YValues = pYValues(index)
    End Property

Here is the actual snippet of code I am trying to run from a seperate module:

    Sub testArrayLoading()
        ' Create a file dialog object
        Dim fd As FileDialog

        ' Choose destination folder (global resource variable!)
        Set fd = Application.FileDialog(msoFileDialogOpen)
        fd.Show

        ' Create a spectrum object
        Dim mySpectrum1 As Spectrum
        Dim mySpectrum2 As Spectrum
        Set mySpectrum1 = New Spectrum
        Set mySpectrum2 = New Spectrum

        ' Populate each spectrum with data
        mySpectrum1.Import (fd.SelectedItems(1))
        mySpectrum2.Import (fd.SelectedItems(1))

        ' Subtract one spectrum from the other
        mySpectrum1.Subtract (mySpectrum2)
    End Sub

Am I not able to use a class object as a parameter within the same class? or am I supposed to be using a property instead of a sub-routine?

So far I have tried using ByVal and ByRef, and switching the subroutine to a Public Property Set instead. Neither has worked for me. I think I'm just missing something in terms of my understanding of passing custom class objects as parameters.

Thanks for the help,

Michael


Solution

  • Parenthesis. This line...

    mySpectrum1.Subtract (mySpectrum2)
    

    ...should be either...

    mySpectrum1.Subtract mySpectrum2
    

    ...or:

    Call mySpectrum1.Subtract(mySpectrum2)
    

    See What does the Call keyword do in VB6?