Solved

property or method?

Posted on 1998-12-17
4
230 Views
Last Modified: 2013-11-25
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
Comment
Question by:Torus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Expert Comment

by:NickH
ID: 1450909
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
 
LVL 2

Author Comment

by:Torus
ID: 1450910
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
 
LVL 10

Accepted Solution

by:
caraf_g earned 50 total points
ID: 1450911
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
 
LVL 2

Author Comment

by:Torus
ID: 1450912
Thank you.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question