I'm new to class modules, and the compiler seems to be enforcing a correlation of the number of input vars for a Get and Let procedure of the same property. The Let property must always have exactly one more variable (the NewValue) than the Get procedure. So the code I've got below does not work, because of the discrepancy in input vars:

Property Get Length() As Integer

  Length = UBound(mProj, 2)

End Property

Property Let Length(NewVal As Integer, Clear As Boolean)

If NewVal < 0 Then Err.Raise 50000, , "Value cannot be negative."
If Clear = True Then
    ReDim mProj(1, NewVal)
Else
  ReDim Preserve mProj(1, NewVal)
End If

End Function

(i altered a bit for clarity so there might be a minor mistake)

My question is why does this have to be the case? It seems that a person, when setting a property to a certain value, might have an untold number of factors affecting the decision, which should be passed as variables. But to simply retrieve this certain property might require no input variables at all.
It just seems like such a strange issue to a fundamental situation, so I was wondering if anyone knows of a workaround, or if I'm missing something, etc.

Recommended Answers

All 4 Replies

Hi,
Think of a property just as if it were a regular variable. You can get or assign only one value at once.
If you need to pass more than one parameter to a property, instead use a regular Sub procedure.

Hope it helps,

Yes, I agree dmf1978 points, Property is a special feature in VB6. It encapsulates a private member variable inside a class.

For Ex
'in a Class

Private mLength As Integer
Public Property Get Length() As Integer
   Length = mLength
End Property

Public Property Let Length(ByVal vNewValue As Integer)
   mLength = vNewValue
End Property

This is normal, But u can have global variable instead

Public Length as Intger

Here the Problem is "you cannot restrict the Value when it is Public variable" but you can do it in Properties.

Get and Let/Set Properties represents the Same thing. SO the Compiler enforce you to the Correct Syntax.

If you want give so many Option You can go For Procedure or Function.
This is available in Other Languages to Access the private Member GetXXX() and SetXXX() Functions.
So the AboveCode Can be

Private mLength As Integer
Public Function GetLength() As Integer
   GetLength = mLength
End Sub 
Public Sub SetLength(ByVal vNewValue As Integer)
   mLength = vNewValue
End Sub

but you need to Mention the function name while accessing it thru object
For Ex

Dim C as New Class1
  Dim Length as Integer

  C.SetLength 100   'Instead of C.Length = 100
  Length = C.GetLength() 'Instead of Length = C.Length

I hope this is helpful somewhat to you.

Hi,

Yes, I concur with both of the above...
for Property , you can Get or Let only one Value parameter.
Since, you are wanting to pass 2 parameters, Create SetLength, as a Public Sub Procedure and not a Property....

Public Sub SetLength(NewVal As Integer, Clear As Boolean)
If NewVal < 0 Then Err.Raise 50000, , "Value cannot be negative."
If Clear = True Then
    ReDim mProj(1, NewVal)
Else
  ReDim Preserve mProj(1, NewVal)
End If
End Sub

Code for GetLength remains the same..


Regards
Veena

Thanks for the responses. I guess I understand that the Get and Let procedures serve specific purposes, and aren't supposed to be substitutes for procedures in general. I had already started making a SetLength procedure before these responses, but now seeing that is what you guys would also do, I know I'm on the right track. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.