0

I have a Sub in a regular Module where my code is mainly taking place. In a class module i have a sub, requiring Arguments which are not properties of the class module itself. Now i have trouble with getting the code started.

I made an example code with a minimum of lines, so you can see what i planned to do.

First the class module "clsCity":

Option Explicit

Public area As Single

'Public l As Single 'Working, but not desired solution:
'Public h As Single 'Working, but not desired solution:

Sub calcArea(length As Single, height As Single)

area = length * height

End Sub

Now the program itself:

Sub Country()

Dim BikiniBottom As New clsCity  
Dim l As Single
Dim h As Single


Bikinibottom.calcArea(l,h) 'Error: "vba: Compile Error: expected: ="

'Working, but not desired solution:
'Bikinibottom.calcArea 'Where l and h are properties of clsCity

End Sub

As you can see, i plan to calculate the area of Bikinibottom with variables "l" and "h", which are not properties of clsCity. I get the error "vba: Compile Error: expected: =" when pressing enter. What do i do wrong? This is a sub and not a function, a "=" should not be necessary.

I know it would work to use the commented code. However, this is just a short example, please keep in mind that in my real code it is not a sophistic solution to make "l" and "h" a property of clsCity (they are properties of other class modules!)

Charlex
  • 33
  • 1
  • 4
  • You are using Bikini bottom.calcarea as a function rather than a sub because you have put the arguments in brackets. This is why you are getting the error message. If you remove the brackets all should be ok. – freeflow Jan 12 '19 at 11:56
  • Hey, thank you for your answer. Without Brackets VBA does not know what parameter i want to use for the calculation. "BikiniBottom.calcArea" is what you mean, correct? – Charlex Jan 12 '19 at 12:42

2 Answers2

0

Ok i managed to do it this way:

in class module:

Function calcArea(length As Single, height As Single)

calcArea = length * height

End Function

and in my main code:

BikiniBottom.area = BikiniBottom.calcArea(l, h)

So basicly my problem is solved. Still curious how it is possible to use Subs with Arguments in a class module. Ideas?

Charlex
  • 33
  • 1
  • 4
  • It does not matter where the sub is. When you call a sub, you don't provide parentheses unless you are using `Call`. `Bikinibottom.calcArea l, h` or `Call Bikinibottom.calcArea(l,h)`. – GSerg Jan 12 '19 at 14:15
0

I'd write this

Option Explicit

Sub Country()

    Dim BikiniBottom As New clsCity

    BikiniBottom.Length = 2
    BikiniBottom.Height = 3

    Debug.Print BikiniBottom.area

End Sub

and then for the class I'd use property procedures ...

Option Explicit

Private m_Length As Single
Private m_Height As Single

Public Property Let Length(rhs As Single)
    m_Length = rhs
End Property
Public Property Get Length() As Single
    Length = m_Length
End Property

Public Property Let Height(rhs As Single)
    m_Height = rhs
End Property
Public Property Get Height() As Single
    Height = m_Height
End Property

Public Function area() As Single
    area = m_Length * m_Height
End Function
S Meaden
  • 8,050
  • 3
  • 34
  • 65