fbpx

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

  • Wolfgang Riedmann
  • Wolfgang Riedmann's Avatar Topic Author
  • Offline
More
2 years 5 months ago #1 by Wolfgang Riedmann
code independent from /az option (0-based arrays) was created by Wolfgang Riedmann
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

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

More
2 years 5 months ago #2 by Karl Faller
Replied by Karl Faller on topic code independent from /az option (0-based arrays)
Curious:is there a hidden reason for the "dummy"-Addition (0+__Arraybase__)?

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

  • Wolfgang Riedmann
  • Wolfgang Riedmann's Avatar Topic Author
  • Offline
More
2 years 5 months ago #3 by Wolfgang Riedmann
Replied by Wolfgang Riedmann on topic code independent from /az option (0-based arrays)
Hi Karl,

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

Wolfgang

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

More
2 years 4 months ago #4 by Robert van der Hulst
Replied by Robert van der Hulst on topic code independent from /az option (0-based arrays)
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
This email address is being protected from spambots. You need JavaScript enabled to view it.

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

  • Wolfgang Riedmann
  • Wolfgang Riedmann's Avatar Topic Author
  • Offline
More
1 year 9 months ago #5 by Wolfgang Riedmann
Replied by Wolfgang Riedmann on topic code independent from /az option (0-based arrays)
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

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