8

I've tried to create a vb scripts class with a constant and got 800A03EA error. It's it a VBS bug? Isn't it an OOP fundamental rule?

Class customer
   ' comment it const and its works
   const MAX_LEN=70

   Private Name

   Private Sub Class_Initialize
      Name = ""
   End Sub

   ' name property.
   Public Property Get getName
      getName = Name
   End Property

   Public Property Let letName(p_name)
      Name = p_name
   End Property
end class
gwarah
  • 241
  • 2
  • 10

3 Answers3

16

The documentation lists all statements that are allowed in the context of classes. Const isn't among them, so it's not supported. You can work around the issue by using private member variables that you initialize during instantiation (i.e. in Class_Initialize):

Class customer
  Private MAX_LEN
  Private Name

  Private Sub Class_Initialize
    MAX_LEN = 70
    Name = ""
  End Sub

  ...
End Class

If instances of the class should expose this value, you could implement it as a read-only property:

Class customer
  Private MAX_LEN

  Private Sub Class_Initialize
    MAX_LEN = 70
  End Sub

  'read-only property, so no "Property Let/Set"
  Public Property Get MaxLength
    MaxLength = MAX_LEN
  End Property

  ...
End Class

However, as Ekkehard.Horner pointed out correctly, the value could still be changed by object-internal code. If immutability is the primary requirment for this value you should implement it as a global constant.

Community
  • 1
  • 1
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • 4
    IMO, stating that the documentation lists all the statements that may appear within a class is quite a bit of a stretch. I have read the documentation three times, and nowhere does it indicate that the list is exhaustive. I think a more accurate statement is that Microsoft implied as much by omitting it. If so, then this joins a long list of such omissions. This is why it should never be left to programmers to write their own documentation; it is perilously easy to omit something important from it. – David A. Gray Jan 02 '16 at 21:46
  • The `MAX_LEN` in `MaxLength = MAX_LEN` will mean outer constant if there is a global `Const MAX_LEN = 0` outside. See [VBScript Class member variables was override by outer constants](https://stackoverflow.com/q/75986906/988089) – cuixiping Apr 14 '23 at 07:52
12

I agree with Ansgar Wiechers's answer, but would like to propose another option.

If immutability is more important than performance, you could put the value directly in the Get and use the property to refer to the value instead of a class-level variable.

Class customer

  'read-only property, so no "Property Let/Set"
  Public Property Get MaxLength
    MaxLength = 70
  End Property

  ...
End Class
Community
  • 1
  • 1
Riley Major
  • 1,904
  • 23
  • 36
4

A Private variable (perhaps with a getter) gives you a value that is read-only from the outside of the class, but class internal code can still change that value.

So using a global Const (perhaps with a 'namespace' name part) may be a better workaround in cases where the constness is most important.

Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96