Solved

property or method?

Posted on 1998-12-17
4
225 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi component that can load a DLL in design time? 8 53
Adding to a VBA? 6 61
Access Object Property from VBA Module in Excel 2010 2 27
Hide vba in gp 7 82
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

910 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

23 Experts available now in Live!

Get 1:1 Help Now