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

TOPIC: Zero / One based arrays - finding out which !?

Zero / One based arrays - finding out which !? 1 year 4 months ago #1

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
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.

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

Zero / One based arrays - finding out which !? 1 year 4 months ago #2

Hi Phil,

checkout __ARRAYBASE__. It should be 0 or 1.

Frank

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

Zero / One based arrays - finding out which !? 1 year 4 months ago #3

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

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

Zero / One based arrays - finding out which !? 1 year 4 months ago #4

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
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.
Attachments:

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

Zero / One based arrays - finding out which !? 1 year 4 months ago #5

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

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

Last edit: by Frank Maraite. Reason: Added information

Zero / One based arrays - finding out which !? 1 year 4 months ago #6

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
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.
Attachments:

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

Zero / One based arrays - finding out which !? 1 year 4 months ago #7

  • wriedmann
  • wriedmann's Avatar
  • Away
  • Posts: 1423
  • Karma: 6
Hi Phil,

please look at this code:
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
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.

Zero / One based arrays - finding out which !? 1 year 4 months ago #8

Wolfgang,

cool!

Frank

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

Zero / One based arrays - finding out which !? 1 year 4 months ago #9

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
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.

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

Zero / One based arrays - finding out which !? 1 year 4 months ago #10

  • wriedmann
  • wriedmann's Avatar
  • Away
  • Posts: 1423
  • Karma: 6
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
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
Attachments:

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

Zero / One based arrays - finding out which !? 1 year 4 months ago #11

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
Hi Wolfgang,

yes, of course this is a possible issue ;-0)

just because a method is STATIC does not say that it MUST be housed / kept in a class library outside of the current Project.

After all, a STATIC method is really just a function.

Can you think of any other useful properties which are project dependant and likely to need my attention ?

Regards,
Phil.

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

Zero / One based arrays - finding out which !? 1 year 4 months ago #12

  • wriedmann
  • wriedmann's Avatar
  • Away
  • Posts: 1423
  • Karma: 6
Hi Phil,

Can you think of any other useful properties which are project dependant and likely to need my attention ?


If I look at all the compiler options, this one seems to be the only dangerous one.
Change it in a working application and the application will not work anymore....

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.

Zero / One based arrays - finding out which !? 1 year 4 months ago #13

Phil,
Wolfgang

as long as you have your unit tests working there is nothing dangerous.

Phil, during the time your are searching a solution you could have changed hundreds or thousands lines of code to the zero based approach. I bet in most cases it is from
for i := 1 to count
to
for i := 0 to count-1
only.
Count the key strokes. Then you know what is faster. And think about compiler pragmas. They are not spported now but I hope in the next future. Wait until this. Then you can change class for class, entity for entity, switching az on and off within your code.

All other approaches lead into confusion I think and make it bit harder to change to collections. I would avoid any tricks even I said cool! to Wolfgangs solution. But thinking a while on it: who knows in a few years what's behind FirstIndex? Is this only the zero thing or does it have context dependend meaning? From my point of view it makes code less readable.

Frank

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

Zero / One based arrays - finding out which !? 1 year 4 months ago #14

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
Frank, Wolfgang, all,

I appreciate that this is an issue which needs some careful thought, however, as a start I can't see anything too wrong with what I have working in the sample shown in the two images below. It should work for all types of .NET array.





Polymorphism works in our favour on line 209 :P

Have a nice evening,
Phil.

P.S. please realise that the inserted code is still just work in progress for the Cologne conference sessions in April 2018.
Attachments:

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

  • Page:
  • 1