Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 235
  • Last Modified:

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
0
Torus
Asked:
Torus
  • 2
1 Solution
 
NickHCommented:
The basic reason for using either properties or methods to return class variables is to protect the class variable from external program trampling...conflicting variable names occur more frequently than we might wish or design.  That said, you are right in assessing the fact that a method can be made to act like a property...in fact, a property is essentially  three built in methods, let, get, and set.  THe advantage to using a property is this:  in the program outside the class, you can assign or retrieve a property by saying

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.  
0
 
TorusAuthor Commented:
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
0
 
caraf_gCommented:
It really doesn't matter whether you use a function or a Property Get. The end result is exactly the same. In the same vein you might use a Property Let in a similar way to using a Sub with exactly one parameter.

When you use what exactly is purely a question of what seems "right" at the time.

I would use a property when the user would expect to use the procedure name both on the left and on the right hand side of an equal sign:

MyClass.MyProperty = Value
Value = MyClass.MyProperty

In very simple cases you could get away with simply defining a public variable of a certain type inside your class/form:
Public MyProperty As <type>

But as soon as you have to start worrying about considerations such as "allowed" values, "translating" values, changing other properties as a result of changing this property and so on, you're better off using Property Let and Property Get as you can handle these things within those procedures.

Hope this helps. Sorry for answering so late but your question has moved down the line quite a bit and it is only because I decided to have a look at the lower end of the scale that I spotted it.
0
 
TorusAuthor Commented:
Thank you.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now