Attribute Usage in Computed Property?

The code is in the Get Method of Computed Property

As you can see i have written attributes with the values but when i run the program it doesn’t
Display myName in MessageBox

Do you have code that ‘gets’ the value of computedProperty1?

eg x = computedproperty1

Yes here it is

Please check the last line

Also the code works fine but as you can see the MsgBox in the Get Method doesn’t display my Name “Gourav Das” which i have set using The Attribute thing on the right

That’s not the way to use attributes. It would be better to make myName and myAge properties and set their default values.

1 Like

please tell me what is the use of that attribute thing on the right when I opened Get And Set method in any Computed Property

I thought it wasnt displaying a messagebox at all.
It IS displaying the contents of your variable called MyName
You havent set that to have any value

A computed property either masks a private variable, or generates a values based on code.

So you may start with an ordinary string called MyName
If you convert that to a computed property, you end up with a private variable called _MyName or similar.
The SET method takes the passed value and puts it into _MyName
The GET method returns a string, and if nothing else is done, it will return _MYName

So if you wanted a message to appear (bad idea unless you are just testing) - you would
MsgBox _MyName

Not a locally declared variable MyName that you didn’t assign a value to.
Attributes are something else again.

1 Like

yes i understood your point.

yet my question is what is the function of those attributes that we see on the right. Can you give an example program , so i that i can understand its use.

AFAIK it’s for the IDE and compiler.

For instance, if you have a old function and a new function, you can set the attribute “deprecated” on the old method and include the name of the new method. When the project is analyzed it will identify all the places that the old method is used and recommend that it be replaced with the new method.

1 Like

https://documentation.xojo.com/getting_started/object-oriented_programming/advanced_oop_features.html

I can’t think of a use for these myself… the docs say its mostly about introspection, which I have never had a use for.

’ Accessing an Attribute

Attributes are accessed in your code using Introspection. The AttributeInfo class is used to fetch the Name-Value attribute pairs for a particular object.

1 Like

Attributes are used with Xojo’s introspection feature.

1 Like

Thank you for the link , :slight_smile:

okay thanks will check out that feature

Attributes are a very fringe case usage. 99.9% of the time, you don’t need them. There are usually better ways to implement what you want.

1 Like

FWIW, one of the uses of Attributes is to mark things as Deprecated when refactoring code. What you can do is set Deprecated as the name and ”methodName” as the replacement method (assuming there is a replacement, notice the quotes on the value). If you do this, when you analyze the project you’ll see uses of this item tagged in the results.

Another common attribute is for hiding things from AutoComplete and Introspection. In this case, all you need to do is set an attribute with the name Hidden. The use for this is typically for writing classes or controls that you’re giving to others when you need a method or property to be public so it can be called or set from outside of the class, but shouldn’t be used by the end user themselves.

3 Likes

… and very useful if you are using a lot of computed properties with private properties underneath and want to declutter the debugger information. With a “hidden” Tag in the attributes of the private properties you will only see the values once, not twice in form of the private property and the getter value.

1 Like

Another note, in recent versions of the IDE, you should be able to right-click in the attributes panel and get a list of common attributes to add. Just keep in mind that there’s a bug (which I hope to fix soon) when setting attributes on multiple items where they don’t commit. You can fix that for now by just clicking the + button on the next line as if you were going to add another attribute and then removing it.

1 Like

Can you please tell me what are the different Attributes available? Like you mentioned Depreacated

Is there any List of pre defined attribute?

Anyways Thanks that clears up a lot :slight_smile:

There are three:

  • Deprecated - For marking things that shouldn’t be used any more and whether or not a replacement exists.
  • Hidden - For hiding things from Introspection, Autocomplete and the Library
  • StructureAlignment - When using structures, you will sometimes find that they need to be aligned on 2, 4 or 8 bytes to match an API that you are accessing with a declare.
  • DefaultEvent - Sets the event that will be selected by default when adding an event to a control or class instance.
3 Likes

UserGuide:Advanced OOP Features - Special Attributes

2 Likes