0

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

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Michael Molter
  • 1,296
  • 2
  • 14
  • 37

1 Answers1

1

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?

Community
  • 1
  • 1
Comintern
  • 21,855
  • 5
  • 33
  • 80
  • Sorry, that was embarrassingly simple! I'm still used to C# syntax. Is this something I should be writing as a property anyway? Then would I be able to call it as `mySpectrum1.Subtract(mySpectrum2)`? Just switching it to `Public Property Set` from `Public Sub` gives me an error. Anyway, Thanks! – Michael Molter Apr 14 '15 at 03:37
  • @MichaelMolter Public Property Set would only be for Objects (Public Property Let would be for primatives). Also, you probably wouldn't want it as a property because the syntax would be weird if it was a setter, i.e. `mySpectrum1.Subtract = mySpectum2`, and either the same: `mySpectrum1.Subtract(mySpectrum2)` if you discard the return value, or an additional reference assignment: `mySpectrum1 = mySpectrum1.Subtract(mySpectrum2)`. Only thing I'd change would be the parameter name - "Other" is the more traditional name. If you prefer `mySpectrum1.Subtract(mySpectrum2)` put the "Call" in front. – Comintern Apr 14 '15 at 03:48