Visual Basic 6 had the notion of a “default property” for controls and classes. Microsoft defined them this way:
“A default property is a class or structure property that your code can access without specifying it. When calling code names a class or structure but not a property, and the context allows access to a property, Visual Basic resolves the access to that class or structure’s default property if one exists.”
For instance, you could assign a label’s caption property like this:
lbl.Caption = "SomeCaptionText"
or you could do:
lbl = "SomeCaptionText"
This was pretty handy. I’ve noticed that in Xojo I always have to use the longer syntax:
obj.property = something
Is there a way in Xojo to use the shorter syntax:
obj = something
If you have a class that is designed to allow “Operator_xxx” overloading, can you also provide a way to assign and retrieve a value directly to the object rather than having to specify obj.Value = x and x = obj.Value?
This does not exist in the built in Xojo classes.
As Jason said, Xojo does not have this concept of a “default property” for controls like VB did.
However, you can simulate it in your own classes by using Operator Overloading. So you could create a Person class and be able to assign a value to its FullName property like this:
Dim p As New Person
p = "Luke Skywalker"
To get the above to work, the Person class needs a FullName As String property and an Operator_Convert method.
Public Sub Operator_Convert(value As String)
Self.FullName = value
End Sub
[quote=420913:@Paul Lefebvre]As Jason said, Xojo does not have this concept of a “default property” for controls like VB did.
However, you can simulate it in your own classes by using Operator Overloading. So you could create a Person class and be able to assign a value to its FullName property like this:
Dim p As New Person
p = "Luke Skywalker"
To get the above to work, the Person class needs a FullName As String property and an Operator_Convert method.
Public Sub Operator_Convert(value As String)
Self.FullName = value
End Sub
[/quote]
Thanks Paul!, That’s really cool! I just tried it and it works great. I looked at all of the “Operator_xxx” items and assumed I would find one like “Operator_Assign” to get the job done. “Operator_Convert” didn’t grab my attention at all.
Xojo is pretty friggin cool.
Thanks again.
Paul,
It would be awesome if you could do a webinar on operator overloading. Some of the operator overloads look interesting but I’m not clear when you might want to use them.
Clarification of these in particular would be great: Operator_Subscript, Operator_Redim and all of the Operator_xxxRight varieties.
I did a webinar that covered operator overloading last summer:
Cool Xojo Language Features
A couple example projects are here:
Examples/Language Features/OperatorLookup
Examples/Language Features/OperatorOverload
I wouldn’t go crazy trying to use every Operator_* method or you may end up with some ugly code. I find Operator_Lookup, Operator_Convert and Operator_Compare are most useful. The others are more special purpose.