I don’t understand “Assigns” and would like to change it to something else, or be able to use it better.
“It” is a set of pieces of code in The Recent Items Article in RealBasic Developer issue 2.1
There is in the “RecentItemsManager” class a method or pair of methods (getter / setter) under the heading Data.
The Data has a disclosure triangle (like a computed property).
??? I don’t know how to add this from the IDE. How?
??? I also don’t understand how it knows to access one over the other
Data is accessed twice in the code. When it reads the data in and out. (for storage on the hard drive)
Input:
If ts <> nil then//In the App.Open
me.RecentItems.Data = ts.ReadAll
ts = nil
End if
Output:
If Ir <> nil then//In the App.Close
Ir.Write DefineEncoding(me.RecentItems.Data, Encdng)//*************
Ir = nil
me.RecentItemsFile.Delete
tempFile.MoveFileTo me.RecentItemsFile.Parent
me.RecentItemsFile.Parent.Child(tempFile.Name).Name = me.RecentItemsFile.Name
End if
The Callouts for the related methods:
Data Input callout is:
Public Sub Data(Assigns data as String)
//Some code for reading from disc and converting
End Sub
Data Output callout is:
Public Function Data() as String
//Some code for writing to disc
End Function
??? I know the input knows which one to go to because of “Assigns” and the = in the statement, but that is about it.
Not sure what other questions to ask
Public Sub Data(Assigns data as String)
//Some code for reading from disc and converting
End Sub
Data Output callout is:
Public Function Data() as String
//Some code for writing to disc
End Function
As COMPUTED PROPERTY
Private Property DiskStuff as String
Get
dim str as string
// put code here to get string data
return str
End Get
Set
// put code here to write "value" to disk
End Set
End Property
Thanks Dave.
On thinking my post over, I only need a couple of things.
I am also changing the title from “Convert Assigns to ???” because it’s easy to convert this.
How to create this in the IDE?
What do you call this?
Thanks. My understanding of a setter is the Setter happens therefore something happens to the Getter. Nothing happens in relation to the other half.
I have no problem in calling it Computed Property. It is just weird that something can be organized this way and not affect the other side.
I’m not sure this has a name, but the technique combines method overloading with the Assigns keyword to emulate the syntax of setting/getting a computed property. You would use this if you want subclasses to be able to override the implementation with their own version (properties can’t be overridden), if you want to get/set an array (computed properties can’t be arrays), or if you need to pass extra parameters (computed property getters and setters can’t have custom parameters).
Add two methods with the same name but different parameters/return type. This is method overloading. Make one method return the value (the getter) and the other one accept the value as a parameter with the Assigns keyword (setter).
It follows the normal rules of method overloading: the version to be used is decided by comparing the order and types of the actual arguments you use in the call to the order and types of the parameters defined by the method. The compiler will error out if it can’t tell which one is meant based on the syntax and parameter types.
What I defined is called a “computed property”
there is no overloading involved (what Arthur originally described WAS overloading)
An overload consists of two methods with the same name, where one is a “function” (same as getter), and the other is a method with assigns (same as setter).
A “computed property” can be added via the Insert Menu, and has the advantage (my opinon) in that it is one entry in the Property Inspector (overloads are at least two), and they can be added to the Inspector Behaviour for custom controls (overloads cannot)
[quote=390897:@Arthur Gabhart]Thanks. It is Overloading. http://developer.xojo.com/userguide/oop-design-concepts
Using similar names is something I avoid. It is good to know I can do this, and the IDE is that intelligent.[/quote]
A COMPUTED PROPERTY is NOT overloading, and OVERLOADING is not a computed property
So it is not a matter of “similar names”, it is two different names for two different concepts, both of which CAN have similar functionality, but both of which are designed for different purposes.
Overloading is methods with the same name. You can define several with identical parameters and return values in the IDE but the compiler won’t be able to figure out which on you mean unless you have distinct parameters. You can overload with one as a getter that returns a value and another that is a setter bit you don’t have to use this pattern.