Torus
asked on
property or method?
I don't understand when the property should be used in a class. For example, if a class has a variable
Dim MyValue as integer
public property get GetValue()
GetMyValue = MyValue
end if
However, in this situation, a method declared in the class also can achieve the purpose
public function GetValue() as integer
GetValue = MyValue
end if
Also, for example, a class and 2 variable
Dim X as integer
DIm Y as integer
Public property Get Sum()
sum = X + Y
end if
The property return the sum of 2 valuable. Similar declaring a method also can achive that.
Is it a common practice that property should be used in a class or in a user define control in getting or assign class's variables? Also, in which condition that property should be used instead of the method? What's the actually different between them in the above examples.
Thanks in advance
Dim MyValue as integer
public property get GetValue()
GetMyValue = MyValue
end if
However, in this situation, a method declared in the class also can achieve the purpose
public function GetValue() as integer
GetValue = MyValue
end if
Also, for example, a class and 2 variable
Dim X as integer
DIm Y as integer
Public property Get Sum()
sum = X + Y
end if
The property return the sum of 2 valuable. Similar declaring a method also can achive that.
Is it a common practice that property should be used in a class or in a user define control in getting or assign class's variables? Also, in which condition that property should be used instead of the method? What's the actually different between them in the above examples.
Thanks in advance
ASKER
Thanks for your answer.
But I still have a question.
As you said , "Let" function may be take the advantage since it needn't pass any arguments. However, I can use function to replace the "GET"
a = myclass.myproperty
Public function myproperty() as integer
myproperty = myvalue
end function
MyProperty can be the function since it also returns the value as the GET does.
For standardisation, maybe it is much good to use GET and LET in pair to get or assign
class's variables.
But what make me dilemma is that if I do some calculations between the class variables and then return the result(As I said in the second examples), property or function should be better used since function also can achieve what property does in same syntax?
a = Myclass.Sum
Public function Sum() as integer
a = MyClassValueX + MyClassValueY
end function
In this situation, what I should decide to use or doesn't matter?
Thanks again
But I still have a question.
As you said , "Let" function may be take the advantage since it needn't pass any arguments. However, I can use function to replace the "GET"
a = myclass.myproperty
Public function myproperty() as integer
myproperty = myvalue
end function
MyProperty can be the function since it also returns the value as the GET does.
For standardisation, maybe it is much good to use GET and LET in pair to get or assign
class's variables.
But what make me dilemma is that if I do some calculations between the class variables and then return the result(As I said in the second examples), property or function should be better used since function also can achieve what property does in same syntax?
a = Myclass.Sum
Public function Sum() as integer
a = MyClassValueX + MyClassValueY
end function
In this situation, what I should decide to use or doesn't matter?
Thanks again
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you.
myclass.myproperty = 23
or
a = myclass.myproperty
whereas a method would be called as:
myclass.set_value 23
or
myclass.get_value
The method example works, but he property approach looks and feels more like true assigment, with the equal sign. Inside the class however, the property manipulations act like methods, and you can do any sort of manipulation or value checking you desire inside the Let and Get properties. The Set proerty is the same, used for Object variables in side the class. It is pretty much customary to use proerties for class variables, as this aids programmers that might come after you in understanding your code.
Hope this helps.