fbpx
Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC:

Indexer Syntax 05 Sep 2018 18:41 #6105

  • orangesocks's Avatar

  • orangesocks

  • Topic Author


  • Posts: 35
  • Hi,
    I'm converting some classes that I made in C# to X# as an exercise to better understand the syntax.
    I'm stuck with the definition of an indexer. I looked in the documentation and I was not able to find anything.

    In C# I would write:
    public <TypeToReturn> this[<TypeToUse> <Value>]
    {
    get { <some code>; }
    set { <some code>; }
    }

    What is the syntax to be used in X# ?

    Regards Giuseppe

    Please Log in or Create an account to join the conversation.

    Indexer Syntax 05 Sep 2018 19:22 #6106

  • Fabrice's Avatar

  • Fabrice


  • Posts: 73
  • Hi Giuseppe,
    it can be something like :
    PROPERTY SELF[ index AS TypeToUse ] AS TypeToReturn
    GET
    LOCAL item AS TypeToReturn
    item := DoWhateverYouWant()
    RETURN item
    END GET
    END PROPERTY

    HTH
    Fab

    Please Log in or Create an account to join the conversation.

    Indexer Syntax 05 Sep 2018 19:42 #6107

  • orangesocks's Avatar

  • orangesocks

  • Topic Author


  • Posts: 35
  • Hi Fab
    It worked like a charm. Thanks a lot.

    Please Log in or Create an account to join the conversation.

    Indexer Syntax 05 Sep 2018 23:17 #6110

  • Chris's Avatar

  • Chris


  • Posts: 2039
  • Guys,

    One additional thing worth mentioning, is that X# supports also regular indexed (named) properties, in addition to indexers:
    FUNCTION Start() AS VOID 
    	LOCAL IMPLIED o := TestClass{}
    	? o:IndexedProperty[123]
    RETURN
    
    CLASS TestClass
    	PROPERTY IndexedProperty[n AS INT]
    		GET
    			RETURN n * 2
    		END GET
    	END PROPERTY
    END CLASS

    I think those are not supported in c# at all, but we had to implement them in X# (and earlier in Vulcan) because VO does support them as well (and they are often very useful).

    Chris
    XSharp Development Team
    chris(at)xsharp.eu

    Please Log in or Create an account to join the conversation.

    Indexer Syntax 06 Sep 2018 10:00 #6115

  • FFF's Avatar

  • FFF


  • Posts: 976
  • Chris,
    out of curiosity, what's the difference to using a method - apart from braces vs. brackets ;-?
    Regards
    Karl (X# 2.5b; Xide 1.25; W8.1/64 German)

    Please Log in or Create an account to join the conversation.

    Indexer Syntax 06 Sep 2018 12:45 #6117

  • Chris's Avatar

  • Chris


  • Posts: 2039
  • Hi Karl,

    Under the hood not much, both actually are implemented like methods internally. What changes is the syntax used to access/assign the indexer from user code:
    LOCAL IMPLIED o := TestClass{}
    ? o:IndexedProperty[123]
    o:IndexedProperty[123] := 10 // if we add a SEtter to our property as well

    Granted, you could recode things so that it uses a method (actually 2 methods, one for reading and one for writing), but in many cases the PROPERTY syntax is more intuitive. It's the same thing as with regular properties, what's the difference of having oWindow:Caption instead of oWindow:GetCaption() and oWindow:SetCaption(cNewCaption) ?

    Chris
    XSharp Development Team
    chris(at)xsharp.eu

    Please Log in or Create an account to join the conversation.

    • Page:
    • 1