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 !?
- Phil Hepburn
- Posts: 743
- Joined: Sun Sep 11, 2016 2:16 pm
-
- Posts: 178
- Joined: Sat Dec 05, 2015 10:44 am
- Location: Germany
Zero / One based arrays - finding out which !?
Hi Phil,
checkout __ARRAYBASE__. It should be 0 or 1.
Frank
checkout __ARRAYBASE__. It should be 0 or 1.
Frank
-
- Posts: 178
- Joined: Sat Dec 05, 2015 10:44 am
- Location: Germany
Zero / One based arrays - finding out which !?
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
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
- Phil Hepburn
- Posts: 743
- Joined: Sun Sep 11, 2016 2:16 pm
Zero / One based arrays - finding out which !?
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 ?
Could a problem have anything to do with the missing CRLF #define problem issue ?
HELP !!!
Phil.
However when I tried '__ARRAYBASE__' I got the attached error. I am using two underscores before and after - is that right ?
Could a problem have anything to do with the missing CRLF #define problem issue ?
HELP !!!
Phil.
-
- Posts: 178
- Joined: Sat Dec 05, 2015 10:44 am
- Location: Germany
Zero / One based arrays - finding out which !?
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
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
- Phil Hepburn
- Posts: 743
- Joined: Sun Sep 11, 2016 2:16 pm
Zero / One based arrays - finding out which !?
Thanks Frank,
yes, your suggestion of ':' worked for me - see small image attached.
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.
yes, your suggestion of ':' worked for me - see small image attached.
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 !?
Hi Phil,
please look at this code:
Wolfgang
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 Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
-
- Posts: 178
- Joined: Sat Dec 05, 2015 10:44 am
- Location: Germany
Zero / One based arrays - finding out which !?
Wolfgang,
cool!
Frank
cool!
Frank
- Phil Hepburn
- Posts: 743
- Joined: Sun Sep 11, 2016 2:16 pm
Zero / One based arrays - finding out which !?
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.
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 !?
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
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 93 times
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it