Working with .Net Arrays
Arrays in Xbasic use one-based indexing. In other programming languages, arrays are zero-indexed. In this guide, you will learn how to work with .Net arrays in Xbasic.
By default, Xbasic arrays are indexed using one-based indexing. This means that the items in the array are referenced using indexes beginning with one (1) for the first entry and continuing on with 2, 3, 4 and so on.
Other environments, including .Net arrays and collections, use zero-based indexing. This means that the first item in the array is referenced with an index value of zero (0) and continues on with 1,2,3 and so on.
When accessing an array or collection in .Net from Xbasic, Alpha Anywhere automatically maps one-based indexes in Xbasic array references (square brackets – "") to zero-based indexes. This removes the need to remember whether an array reference is for an Xbasic array or a .Net array or collection.
- Xbasic square bracket references ("x" for example) to.Net collections and indexed properties assumes one-based indexing on the Xbasic side and maps to zero-based indexing on the .Net side.
- This is consistent with the default behavior of Xbasic arrays.
- If a .Net collection or class uses a function to access items, the Xbasic function syntax ("x.Item(0)" for example) does not map any parameters, as it does not know if they are indexes or simple values.
- If you call a .Net function, consult the documentation as to appropriate values. In general, indexing functions will require that you use zero-based indexing.
The interactive window session below now shows accessing the System::Data::DataSet Tables collection using the Item property with a numeric or string index:
dim ds as system::data::dataset t1 = ds.Tables.Add("Fred1") ?t1.TableName = "Fred1" t2 = ds.Tables.Add("Fred2") ?t2.TableName = "Fred2" Tables = ds.Tables.Count for i = 1 to Tables ; ? ds.Tables.Item[i].TableName + crlf(); next Fred1 Fred2 ?ds.Tables.Item["Fred1"].TableName = "Fred1" ?ds.Tables.Item["Fred2"].TableName = "Fred2"