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

TOPIC:

code independent from /az option (0-based arrays) 12 Aug 2016 05:40 #302

  • wriedmann
  • wriedmann's Avatar
  • Topic Author


  • Posts: 3245
  • The best option to write code independent from the /az compiler option (use 0-based arrays) is to use foreach:
    local aData as string[]
    	
    aData := <string>{ "one", "two", "three", "four", "five", "six", "seven" }
    foreach cString as string in aData
        System.Console.WriteLine( String.Format( "member is {0}", cString ) )
    next

    but if you cannot for some reason, the following code works:
    local aData as string[]
    local nLen as int
    local nI as int
    	
    aData := <string>{ "one", "two", "three", "four", "five", "six", "seven" }
    nLen := aData:Length - 1 + __ARRAYBASE__
    for nI := ( 0 + __ARRAYBASE__ ) upto nLen
       System.Console.WriteLine( String.Format( "member {0} is {1}", nI:ToString(), aData[nI] ) )
    next
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it

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

    code independent from /az option (0-based arrays) 18 Aug 2016 10:41 #306

    • FFF
    • FFF's Avatar


  • Posts: 1392
  • Curious:is there a hidden reason for the "dummy"-Addition (0+__Arraybase__)?
    Regards
    Karl (X# 2.14.0.4; Xide 1.33; W8.1/64 German)

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

    code independent from /az option (0-based arrays) 18 Aug 2016 20:26 #307

    • wriedmann
    • wriedmann's Avatar
    • Topic Author


  • Posts: 3245
  • Hi Karl,

    yes - stupdity <g>. __ARRAYBASE__ alone would be more than enough.

    Wolfgang
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it

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

    code independent from /az option (0-based arrays) 19 Aug 2016 16:08 #310

    • robert
    • robert's Avatar


  • Posts: 3286
  • karl, Wolfgang,
    The compiler will optimize (fold) the expression 0 + __ARRAYBASE__ into one numeric constant, regardless of the setting of /az.


    Robert
    XSharp Development Team
    The Netherlands

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

    code independent from /az option (0-based arrays) 07 Apr 2017 10:26 #1602

    • wriedmann
    • wriedmann's Avatar
    • Topic Author


  • Posts: 3245
  • Hello,

    when trying to write a version of Proper() that is independent from the /az compiler option, I had to learn that a string is NOT an array, and therefore all indexes are 0-based:
    cString[cString:Length]
    will give a runtime error, whereas
    cString[0]
    not.

    And this is my Proper() extension method:
    static class StringExtensions
    static method Proper( self cString as string ) as string   
      local cReturn as string
      local aChars as char[]
      local lNewWord as logic
      local nI as int
      local nLen as int   
      local cCurrent as Char
    	
      if String.IsNullOrEmpty( cString )
        return cString
      endif                         
      nLen := cString:Length
      aChars := char[]{ nLen }
      lNewWord := true      
      --nLen
      for nI := 0 upto nLen
        cCurrent := cString[nI]
        if char.IsLetterOrDigit( cCurrent )
          if lNewWord
            lNewWord := false
            cCurrent := Char.ToUpper( cCurrent )
          else
            cCurrent := Char.ToLower( cCurrent )
          endif
        else
          lNewWord := true
        endif
        aChars[nI+__ARRAYBASE__] := cCurrent
      next
      cReturn := string{ aChars }
    	
      return cReturn      
    	
    end class

    Wolfgang
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it

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

    • Page:
    • 1