Zero / One based arrays - finding out which !?

This forum is meant for questions and discussions about the X# language and tools
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Zero / One based arrays - finding out which !?

Post by Phil Hepburn »

Hi TEAM,

Please tell me how to find out in X# code whether the project I am coding has its property for 'Use Zero Based Arrays' set to True or False.

I would like to write array handling code which works whatever this setting.

Is there a smart way / recommended way to write such smart code ?

Cheers and TIA,
Phil.
Wales, UK.
Frank Maraite
Posts: 176
Joined: Sat Dec 05, 2015 10:44 am
Location: Germany

Zero / One based arrays - finding out which !?

Post by Frank Maraite »

Hi Phil,

checkout __ARRAYBASE__. It should be 0 or 1.

Frank
Frank Maraite
Posts: 176
Joined: Sat Dec 05, 2015 10:44 am
Location: Germany

Zero / One based arrays - finding out which !?

Post by Frank Maraite »

Hi Phil,

for your second question: In my opinion it does not make sense to mix thing. It confuses our thinking. One of the first things I did when switchung to Vulcan: I set /az on and changed all array access to zero based. Nowadays my nit tests prevents me doing it wrong.
In most cases it was easy: a simple change for i=1 to x to for i:= 0 to x-1. That's all. Then we got foreach, where index settings aren't such important. The whole world of collections work zero based, and sooner or later we switch from VO-Arrays to ,NET-collections. Then start with /az as soon as possible.

my 2 ct's

Frank
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Zero / One based arrays - finding out which !?

Post by Phil Hepburn »

Thanks Frank, this looks hopeful.

However when I tried '__ARRAYBASE__' I got the attached error. I am using two underscores before and after - is that right ?
ArrayBase_01.jpg
ArrayBase_01.jpg (97.62 KiB) Viewed 345 times
Could a problem have anything to do with the missing CRLF #define problem issue ?

HELP !!!
Phil.
Frank Maraite
Posts: 176
Joined: Sat Dec 05, 2015 10:44 am
Location: Germany

Zero / One based arrays - finding out which !?

Post by Frank Maraite »

Hi Phil,

are you sure you have X#/core ?
Oh: Write __ARRAYBASE__:ToString()

With '.' the compiler is searching for a class __ARRAYBASE__ with static method ToString().

Frank
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Zero / One based arrays - finding out which !?

Post by Phil Hepburn »

Thanks Frank,

yes, your suggestion of ':' worked for me - see small image attached.
ArrayBase_02.jpg
ArrayBase_02.jpg (37.65 KiB) Viewed 345 times
In a day or two I will be back to you to see if there are any other similar smart things I should be doing to help guys moving to .NET and using X# setup for VO compatibility.

Speak soon,
Phil.
Wales, UK.
User avatar
wriedmann
Posts: 3649
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Zero / One based arrays - finding out which !?

Post by wriedmann »

Hi Phil,

please look at this code:

Code: Select all

function Start( ) as void
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 := __ARRAYBASE__ upto nLen
  System.Console.WriteLine( String.Format( "member {0} is {1}", nI:ToString(), aData[nI] ) )
next 
for nI := aData:FirstIndex() upto aData:LastIndex()
  System.Console.WriteLine( String.Format( "member {0} is {1}", nI:ToString(), aData[nI] ) )
next 
	
foreach cString as string in aData
  System.Console.WriteLine( String.Format( "member is {0}", cString ) )
next                
	
return                    

static class ArrayHelper       
	
static method FirstIndex( self aArray as System.Array ) as int

return __ARRAYBASE__

static method LastIndex( self aArray as System.Array ) as int
local nLength			as int
		
nLength			:= aArray:Length - 1 + __ARRAYBASE__
		
return nLength
	
end class
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Frank Maraite
Posts: 176
Joined: Sat Dec 05, 2015 10:44 am
Location: Germany

Zero / One based arrays - finding out which !?

Post by Frank Maraite »

Wolfgang,

cool!

Frank
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Zero / One based arrays - finding out which !?

Post by Phil Hepburn »

Thanks Wolfgang,

I actually have something similar to this running successfully.

I intend to make a couple of Static methods to use as 'Lower Bound' and 'Upper Bound' FOR loop values.

Thanks for sharing.

Have a nice weekend,
Phil.
User avatar
wriedmann
Posts: 3649
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Zero / One based arrays - finding out which !?

Post by wriedmann »

Hi Phil,

maybe the static methods are not the best approach, I think.

Think about this case:
- put the array extension class in a separate lib with 0-based arrays
- call it from an application that is compiled with 1-based arrays

and you will see a runtime error.

Using the __ARRAYBASE__ in the application itself this problem does not occurr (of course).

I have added a zip file with both small application to this message (as usual as XIDE export files).

Wolfgang
Attachments
ArrayBase.zip
(2.25 KiB) Downloaded 27 times
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Post Reply