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

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

code independent from /az option (0-based arrays) 2 years 10 months ago #302

  wriedmann's Avatar Topic Author wriedmann Offline Posts: 1545
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
This email address is being protected from spambots. You need JavaScript enabled to view it.
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) 2 years 9 months ago #306

  FFF's Avatar FFF Away Posts: 565
Curious:is there a hidden reason for the "dummy"-Addition (0+__Arraybase__)?

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

code independent from /az option (0-based arrays) 2 years 9 months ago #307

  wriedmann's Avatar Topic Author wriedmann Offline Posts: 1545
Hi Karl,

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

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
This email address is being protected from spambots. You need JavaScript enabled to view it.
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) 2 years 9 months ago #310

  robert's Avatar robert Offline Posts: 969
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.

code independent from /az option (0-based arrays) 2 years 2 months ago #1602

  wriedmann's Avatar Topic Author wriedmann Offline Posts: 1545
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
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

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

  • Page:
  • 1