xsharp.eu • code independent from /az option (0-based arrays)
Page 1 of 1

code independent from /az option (0-based arrays)

Posted: Fri Aug 12, 2016 3:40 am
by wriedmann
The best option to write code independent from the /az compiler option (use 0-based arrays) is to use foreach:

Code: Select all

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:

Code: Select all

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

code independent from /az option (0-based arrays)

Posted: Thu Aug 18, 2016 8:41 am
by FFF
Curious:is there a hidden reason for the "dummy"-Addition (0+__Arraybase__)?

code independent from /az option (0-based arrays)

Posted: Thu Aug 18, 2016 6:26 pm
by wriedmann
Hi Karl,

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

Wolfgang

code independent from /az option (0-based arrays)

Posted: Fri Aug 19, 2016 2:08 pm
by robert
karl, Wolfgang,
The compiler will optimize (fold) the expression 0 + __ARRAYBASE__ into one numeric constant, regardless of the setting of /az.


Robert

code independent from /az option (0-based arrays)

Posted: Fri Apr 07, 2017 8:26 am
by wriedmann
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:

Code: Select all

cString[cString:Length]
will give a runtime error, whereas

Code: Select all

cString[0]
not.

And this is my Proper() extension method:

Code: Select all

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