문제

R 상수로 VB 스크립트 클래스를 만들고 800A03EA 오류가 발생했습니다.그것은 VBS 버그입니까?그것은 oop 근본 규칙이 아닌가?

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
.

도움이 되었습니까?

해결책

설명서 클래스 컨텍스트에서 허용되는 모든 명령문을 나열합니다.Const는 그 중 하나가 아니므로 지원되지 않습니다.인스턴스화 중에 초기화하는 개인 구성원 변수를 사용하여 문제를 해결할 수 있습니다 (즉, Class_Initialize에서) :

Class customer
  Private MAX_LEN
  Private Name

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

  ...
End Class
.

클래스 인스턴스 가이 값을 노출 해야하는 경우 읽기 전용 속성으로 구현할 수 있습니다.

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
.

그러나 ekkehard.horner 가 올바르게 지적되었으므로 객체 내부 코드로 값을 여전히 변경할 수 있습니다.불변성 이이 값에 대한 주요 요구 사항이면 전역 상수로 구현해야합니다.

다른 팁

ansgar wiechers 답변 그러나 다른 옵션을 제안하고 싶습니다.

성능보다 불변성이 더 중요한 경우 GET에 값을 직접 넣고 등록 정보를 사용하여 클래스 수준 변수 대신 값을 참조 할 수 있습니다.

Class customer

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

  ...
End Class
.

개인 변수 (아마도 Getter와 함께)는 클래스 외부에서 읽을 수있는 값을 제공하지만 클래스 내부 코드는 여전히 해당 값을 변경할 수 있습니다.

그래서 global const (아마도 '네임 스페이스'이름 부분과 함께)는 conftness가 가장 중요한 경우 더 나은 해결 방법 일 수 있습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top