xsharp.eu • Zero / One based arrays - finding out which !?
Page 1 of 2

Zero / One based arrays - finding out which !?

Posted: Sat Nov 11, 2017 10:55 am
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.

Zero / One based arrays - finding out which !?

Posted: Sat Nov 11, 2017 11:34 am
by Frank Maraite
Hi Phil,

checkout __ARRAYBASE__. It should be 0 or 1.

Frank

Zero / One based arrays - finding out which !?

Posted: Sat Nov 11, 2017 11:49 am
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

Zero / One based arrays - finding out which !?

Posted: Sat Nov 11, 2017 11:49 am
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 759 times
Could a problem have anything to do with the missing CRLF #define problem issue ?

HELP !!!
Phil.

Zero / One based arrays - finding out which !?

Posted: Sat Nov 11, 2017 11:53 am
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

Zero / One based arrays - finding out which !?

Posted: Sat Nov 11, 2017 12:56 pm
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 759 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.

Zero / One based arrays - finding out which !?

Posted: Sat Nov 11, 2017 3:22 pm
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

Zero / One based arrays - finding out which !?

Posted: Sun Nov 12, 2017 8:53 am
by Frank Maraite
Wolfgang,

cool!

Frank

Zero / One based arrays - finding out which !?

Posted: Sun Nov 12, 2017 1:17 pm
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.

Zero / One based arrays - finding out which !?

Posted: Sun Nov 12, 2017 1:54 pm
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