Some forms of ARRAY do not get their bases set to 0/1 as in Project settings !?

This forum is meant for questions and discussions about the X# language and tools
Frank Maraite
Posts: 178
Joined: Sat Dec 05, 2015 10:44 am
Location: Germany

Some forms of ARRAY do not get their bases set to 0/1 as in Project settings !?

Post by Frank Maraite »

Phil,

you could show us how to avoid issues like you have. You could show us the basic rules:

- avoid magic numbers. Use defines or consts or computed variables instead.

- define a business oriented interface and put the data representation behind, like

Code: Select all

interface IDays
   method GetDayByNumber( number as int ) as Day
end interface

class Days implement IDays
private property _Days as Day[]
   method IDays.GetDayByNumber( number as int ) as Day ; return _Days[number-1]
end class
Later on you can change from array to collection without change any other line of code outside the class definition.

- and of course show the intense use of unit tests to verify it works as intended even after the change to collections.

BTW: you can define an indexed access inside the Days class (and IDays interface of course) by an indexed property where you can do the -1 thing. Look for the special property names 'item' or 'self'. With that you can use Days with the same index syntax as real arrays.

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

Some forms of ARRAY do not get their bases set to 0/1 as in Project settings !?

Post by Phil Hepburn »

Good morning Frank,

It is all very interesting what you write, however, I have been given a topic by Meinhard, on which I must deliver a session in April 2018 at Cologne.

The session title is "From Arrays to Collections", in which I must have explanatory material on what Arrays and Collections really are and how we can code them in X#.

I can't ignore that fact that a Project setting of 'Use Zero Based Arrays' exists and what impact that then has on our code that we write. Or what happens if the setting get changed further down the development path.

You can actually create and use Arrays without a need to refer to indices at all, but that is not really the point. We have to cover what exists.

Take a look at this image - lines 162 through 166 and see how we can access each and every array element of a three dimensional array without needing to use a single index directly.
ZeroBase_11.jpg
ZeroBase_11.jpg (49.65 KiB) Viewed 271 times
The methods used in the lines numbered 147 through 157 are all part of the .NET Framework and are part of the System.Array class. It is the 'Get Bound' methods which seem to be unaware of the Project 0/1 setting.

On a personal note, I try to never use Arrays unless I have no choice, even then there are smarter ways to use them. But we must make sure that what is there works correctly in X# I feel. And that we have a reasonable understanding of how things work.

Best regards,
Phil.
Wales, UK.
User avatar
wriedmann
Posts: 3757
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Some forms of ARRAY do not get their bases set to 0/1 as in Project settings !?

Post by wriedmann »

Hi Frank, hi Karl,

I have adapted my function (or better, extension method) better to the VO equivalent (but kept it 0-based):

Code: Select all

static method SubStr( self cString as string, nStart as int, nLen as int ) as string
// 0-based!!!
local nStringLen as int
local cReturn as string
	
nStringLen := cString:Length	
if nStart < 0
  if ( nStart * -1 ) >= nStringLen
    nStart := 0
  else
    nStart := nStringLen + nStart
  endif
endif
if nLen >= ( nStart + nStringLen )
  cReturn := cString:SubString( nStart )
else
  cReturn := cString:Substring( nStart, nLen )
endif
	
return cReturn
I don't like to check string lengths before retrieving a substring, I prefer the VO behavior.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Some forms of ARRAY do not get their bases set to 0/1 as in Project settings !?

Post by Phil Hepburn »

Frank, Wolfgang, guys,

Whatever happened to the original thread title ?

No the wonder some of us find it difficult to find stuff we remember reading in the past :P

Regards,
Phil.
Post Reply