User Tools

Site Tools


net_array

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
net_array [2018/02/04 18:22] wolfgangriedmannnet_array [2018/09/09 04:20] (current) wolfgangriedmann
Line 8: Line 8:
  
 A static array is defined by any datatype, followed by an open and a close square bracket: A static array is defined by any datatype, followed by an open and a close square bracket:
-<code>local aData as byte[]</code>+<code visualfoxpro>local aData as byte[]</code>
 To initialize such an array, you need to specify also the dimension, or to specify the values, casting to the right datatype: To initialize such an array, you need to specify also the dimension, or to specify the values, casting to the right datatype:
-<code>aData := byte[]{ 10 }+<code visualfoxpro>aData := byte[]{ 10 }
 aData := <byte> { 1, 2, 3, 4, 5 }</code> aData := <byte> { 1, 2, 3, 4, 5 }</code>
 It depends on the compiler settings of your application, if the index of your array starts with ''0'' (like C#) or with ''1'' (like VO). It depends on the compiler settings of your application, if the index of your array starts with ''0'' (like C#) or with ''1'' (like VO).
 Therefore it is better to use the ''foreach'' operator to enumerate a static array. Therefore it is better to use the ''foreach'' operator to enumerate a static array.
-<code>foreach cByte as byte in aData+<code visualfoxpro>foreach cByte as byte in aData
   // Do something   // Do something
 next</code> next</code>
 Since an array is also an object, it has its own properties and methods. Since an array is also an object, it has its own properties and methods.
 But the static methods of the array class are much more interesting: But the static methods of the array class are much more interesting:
-<code>nIndex := Array.IndexOf( aData, 2 )+<code visualfoxpro>nIndex := Array.IndexOf( aData, 2 )
 cByte := Array.Find( aData, { a as byte => a == 2 } ) cByte := Array.Find( aData, { a as byte => a == 2 } )
 cByte := Array.Find( aData, FindByte ) cByte := Array.Find( aData, FindByte )
Line 29: Line 29:
 return false</code> return false</code>
  
 +There are two different types of multidimensional arrays with different syntax:
 +The jagged arrays or arrays of arrays:
 +<code visualfoxpro>local aJagged as string[][]
  
 +aJagged := <string[]>{ <string>{ "X#", "xsharp.info" }, <string>{ "C#", "microsoft.com" } }
 +System.Console.WriteLine( aJagged[0][0] )</code>
 +The jagged arrays are more efficient, but have a (for xBase users) strange syntax to access the members.
 +<code visualfoxpro>local aJagged as string[][]
 +aJagged := <string[]>{string[]{nLen}, string[]{nLen},string[]{nLen} }</code>
 +At this level, there are no strings in aJagged. Only NULL values.
  
 +And now (if zero based), it can be filled: 
 +<code visualfoxpro>aJagged[0…2][0..nLen-1] := "Test"
 +
 +aJagger[0][0] := "First String element in the 1st array"
 +aJagged[1][2] := "Hello !"
 +aJagged[2][nLen-1] := "Last String element in the last array"</code>
 +
 +There are also multidimensional arrays:
 +<code visualfoxpro>local aMulti as string[,]
 +
 +aMulti := string[,]{2,2}{ <string>{ "X#", "xsharp.info" }, <string>{ "C#", "microsoft.com" } }
 +System.Console.WriteLine( aMulti[0,0] )</code>
 +Of course you can use them also in more than 2 dimensions.
 +
 +In the daily coding the [[collections|Collections]] are much more important, because they can be resized.
 +
 +But it is important to know how to deal with arrays because some methods return them or expect them as parameters, and because these are extremely efficient.
 +
 +The VO dynamic arrays pay a high price for their flexibility: every member is an usual (with the known overhead at runtime and in memory) and therefore any type checking must be done at runtime by the programmers code (for the .NET arrays the compiler does these checks).
 +But the X# runtime (ready from June 2018) introduces typed arrays, so you can have both: the flexibility of the VO array and the compiler checks of the .NET array.
 +
 +You can find more help informations about .NET arrays in the MSDN page [[https://docs.microsoft.com/en-us/dotnet/api/system.array|System.Array]]
  
  
net_array.1517768553.txt.gz · Last modified: 2018/02/04 18:22 by wolfgangriedmann