0

I come from a C# background and I'm trying to port some C# code to VBA for use in an excel file.

I have a function that takes a type (many properties) and should do some work on it. Here's a snippet of the code:

Public Function CalculateThrust(ByVal PumpParams As PumpParameters) As AxialThrustValues
    Dim statVars As StaticVariables
    Set statVars = New StaticVariables
    statVars.Init (PumpParams)

    'empty return obj provision
    Dim ret As AxialThrustValues
    Set ret = New AxialThrustValues

    If PumpParams.NumberOfStages > 3 Then
        ret.AxialThrust = -1 * statVars.FAES

My problem is that when I debug, it jumps right to the last variable "FAES" and says:

"Compile error: Method or data member not found"

Maybe I'm missing something with the VBA language but, my though process would be that the function should evaluate as such:

  1. Take "PumpParams" parameter input
  2. Use "PumpParams" to instantiate statVars object
  3. Be able to access statVars.FAES because the object has been instantiated...

Any ideas to help me get past this roadblock would be very much appreciated, thanks.

Snippet of the StaticVariables class:

Private InputParameters As PumpParameters

    Public Sub Init(InputParameters As PumpParameters)
    InputParameters = PumpParameters
    setPropsInitial
    setConditionals
    End Sub

    Public GAM, VISCOS, OMEGA, U2S, U2N, FMS, FMN, CAES, CAHS, CAEN, CAHN, P1S, PST, FAES, FAHS, FAEN, FAHN, FATB, FAIB, PCB, FACB, FAHDCB, FAHSCB, PHDE, PHSE As Double

    Private Sub setPropsInitial()
        FAES = CalcForcesAtImpEye(InputParameters.SuctionImpDia, InputParameters.SuctionStageEyeRingDia, P1S, GAM, U2S, CAES, FMS)...

Pic of debugger: enter image description here

TylerH
  • 20,799
  • 66
  • 75
  • 101
beeker
  • 780
  • 4
  • 17
  • I'm not sure what *it jumps right to the last variable "FAES"* means. Does it highlight the `ret.AxialThrust = -1 * statVars.FAES` line specifically? Are you sure it's not on the `statVars.Init (PumpParams)` line where you must [remove the parentheses](https://stackoverflow.com/questions/57400617/run-time-error-430-occurs-when-copyfromrecordset-is-ran#comment101282858_57400617)? Do you have `Option Explicit` on top? Where does it take you if you place the cursor at `StaticVariables` and press Shift+F2? – GSerg Aug 09 '19 at 21:17
  • Yes, it highlights FAES and shows the error. I added "Option Explicit" (did not have before) but, same error message shows up. Shift+F2 takes me to the "Dim statVars..." line – beeker Aug 09 '19 at 21:19
  • On the `Dim statVars` line, put the cursor on `StaticVariables` and then press Shift+F2. – GSerg Aug 09 '19 at 21:20
  • Over the class name "StaticVariable" + Shift +F2, shows the error "Identifier under cursor is not recognized" – beeker Aug 09 '19 at 21:23
  • Now on the `ret.AxialThrust = -1 * statVars.FAES` line, where does `FAES` take you? – GSerg Aug 09 '19 at 21:25
  • Same, "Identifier under cursor is not recognized" – beeker Aug 09 '19 at 21:26
  • @ComputerVersteher While it is true that they are all Variants and that the parentheses should be removed, it has nothing to do with this problem. Encapsulating as properties is yet another issue that would go down as stylistic choice. – GSerg Aug 09 '19 at 21:26
  • Is the `StaticVariables` class in the same workbook? Check your references under Tools - References, is anything `MISSING:`? – GSerg Aug 09 '19 at 21:27
  • Yes, these are all classes that I wrote myself, no external references – beeker Aug 09 '19 at 21:27
  • Still check the references. Also, does it start working if you declare `Dim statVars As Object`? – GSerg Aug 09 '19 at 21:28
  • Negatory on the instantiating as Object type. Same errors. – beeker Aug 09 '19 at 21:30
  • It cannot be. `Dim statVars As Object` makes this compile time check specifically impossible and turns it into a runtime error in case the member is indeed missing. You only declare the variable as object, you still instantiate `New StaticVariables`. Have you checked the references for `missing` items? – GSerg Aug 09 '19 at 21:32
  • 1
    Add Oprion Explicit on top of StaticVariables. those public vars need to be on top of class (before first method) ot are they inside a method? – ComputerVersteher Aug 09 '19 at 21:33
  • @ComputerVersteher - done but, no change – beeker Aug 09 '19 at 21:35
  • @GSerg - not sure what to look for missing in references. I only see external assemblies there and I'm not really using any of them. The classes I have are all ported over from my C# code and are basically just simple math operations – beeker Aug 09 '19 at 21:35
  • 1
    Yes, of course. Module-level variables must be declared before any methods. Move the `Public GAM, VISCOS, OMEGA, ...` line to be before the `Sub Init` or any other sub in the module. (A missing reference would be denoted by a capital `MISSING:` in front of it; when you have even one, it may break lookup of existing members in completely unrelated assemblies, so you need to always check for that.) – GSerg Aug 09 '19 at 21:36
  • I think that may have done it... Getting an error a bit further down now. Still have some refactoring to do to make sure but looking good, thanks. – beeker Aug 09 '19 at 21:38
  • 1
    Side note re your `Public GAM, VISCOS, OMEGA, ...`. All those except the last one are declared as `Variant` – chris neilsen Aug 09 '19 at 22:24
  • At this point, I think it would be best for you to prepare a [mcve] that demonstrates the issue, with emphasis on *minimal* - something that can be edited into the question so that others can test. Without having the entire structure trying to trouble-shoot this is taking a lot of time without going anywhere. Create a new project with bare-bones of the approach being used for classes, "types" involved that replicates the problem... – Cindy Meister Aug 10 '19 at 04:20

1 Answers1

-2

Two errors at the same time can make our life hard.

First you declared the classes public variables at the wrong place (C# style?), then you used a class member not displayed by intellisense (what means there is no member, except it is late-bound).

Next time you should use the object browser F2 to see the classes members.

Have a look at RubberduckVBA what would revealed that at once I bet and read https://rubberduckvba.wordpress.com/2019/07/08/about-class-modules/ and the other Posts there to see how to write code in VBA.

ComputerVersteher
  • 2,638
  • 1
  • 10
  • 20