Solved

property or method?

Posted on 1998-12-17
4
224 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
  • 2
4 Comments
 

Expert Comment

by:NickH
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now