Export (0) Print
Expand All

Array.GetValue Method

Gets the value of the specified element in the current Array. The indexes are specified as an array of 32-bit integers.

Overload List

Gets the value at the specified position in the one-dimensional Array. The index is specified as a 32-bit integer.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function GetValue(Integer) As Object
[C#] public object GetValue(int);
[C++] public: Object* GetValue(int);
[JScript] public function GetValue(int) : Object;

Gets the value at the specified position in the multidimensional Array.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function GetValue(ParamArray Integer()) As Object
[C#] public object GetValue(params int[]);
[C++] public: Object* GetValue(int __gc[]);
[JScript] public function GetValue(int[]) : Object;

Gets the value at the specified position in the one-dimensional Array. The index is specified as a 64-bit integer.

[Visual Basic] Overloads Public Function GetValue(Long) As Object
[C#] public object GetValue(long);
[C++] public: Object* GetValue(__int64);
[JScript] public function GetValue(long) : Object;

Gets the value at the specified position in the multidimensional Array. The indexes are specified as an array of 64-bit integers.

[Visual Basic] Overloads Public Function GetValue(ParamArray Long()) As Object
[C#] public object GetValue(params long[]);
[C++] public: Object* GetValue(__int64 __gc[]);
[JScript] public function GetValue(long[]) : Object;

Gets the value at the specified position in the two-dimensional Array. The indexes are specified as 32-bit integers.

[Visual Basic] Overloads Public Function GetValue(Integer, Integer) As Object
[C#] public object GetValue(int, int);
[C++] public: Object* GetValue(int, int);
[JScript] public function GetValue(int, int) : Object;

Gets the value at the specified position in the two-dimensional Array. The indexes are specified as 64-bit integers.

[Visual Basic] Overloads Public Function GetValue(Long, Long) As Object
[C#] public object GetValue(long, long);
[C++] public: Object* GetValue(__int64, __int64);
[JScript] public function GetValue(long, long) : Object;

Gets the value at the specified position in the three-dimensional Array. The indexes are specified as 32-bit integers.

[Visual Basic] Overloads Public Function GetValue(Integer, Integer, Integer) As Object
[C#] public object GetValue(int, int, int);
[C++] public: Object* GetValue(int, int, int);
[JScript] public function GetValue(int, int, int) : Object;

Gets the value at the specified position in the three-dimensional Array. The indexes are specified as 64-bit integers.

[Visual Basic] Overloads Public Function GetValue(Long, Long, Long) As Object
[C#] public object GetValue(long, long, long);
[C++] public: Object* GetValue(__int64, __int64, __int64);
[JScript] public function GetValue(long, long, long) : Object;

Example

[Visual Basic, C#, C++] The following code example demonstrates how to set and get a specific value in a one-dimensional or multidimensional array.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of GetValue. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports System

Public Class SamplesArray

   Public Shared Sub Main()

      ' Creates and initializes a one-dimensional array.
      Dim myArr1(4) As [String]

      ' Sets the element at index 3.
      myArr1.SetValue("three", 3)
      Console.WriteLine("[3]:   {0}", myArr1.GetValue(3))


      ' Creates and initializes a two-dimensional array.
      Dim myArr2(5, 5) As [String]

      ' Sets the element at index 1,3.
      myArr2.SetValue("one-three", 1, 3)
      Console.WriteLine("[1,3]:   {0}", myArr2.GetValue(1, 3))


      ' Creates and initializes a three-dimensional array.
      Dim myArr3(5, 5, 5) As [String]

      ' Sets the element at index 1,2,3.
      myArr3.SetValue("one-two-three", 1, 2, 3)
      Console.WriteLine("[1,2,3]:   {0}", myArr3.GetValue(1, 2, 3))


      ' Creates and initializes a seven-dimensional array.
      Dim myArr7(5, 5, 5, 5, 5, 5, 5) As [String]

      ' Sets the element at index 1,2,3,0,1,2,3.
      Dim myIndices() As Integer = {1, 2, 3, 0, 1, 2, 3}
      myArr7.SetValue("one-two-three-zero-one-two-three", myIndices)
      Console.WriteLine("[1,2,3,0,1,2,3]:   {0}", myArr7.GetValue(myIndices))

   End Sub 'Main 

End Class 'SamplesArray


'This code produces the following output.
'
'[3]:   three
'[1,3]:   one-three
'[1,2,3]:   one-two-three
'[1,2,3,0,1,2,3]:   one-two-three-zero-one-two-three


[C#] 
using System;

public class SamplesArray  {

   public static void Main()  {
 
      // Creates and initializes a one-dimensional array.
      String[] myArr1 = new String[5];

      // Sets the element at index 3.
      myArr1.SetValue( "three", 3 );
      Console.WriteLine( "[3]:   {0}", myArr1.GetValue( 3 ) );


      // Creates and initializes a two-dimensional array.
      String[,] myArr2 = new String[5,5];

      // Sets the element at index 1,3.
      myArr2.SetValue( "one-three", 1, 3 );
      Console.WriteLine( "[1,3]:   {0}", myArr2.GetValue( 1, 3 ) );


      // Creates and initializes a three-dimensional array.
      String[,,] myArr3 = new String[5,5,5];

      // Sets the element at index 1,2,3.
      myArr3.SetValue( "one-two-three", 1, 2, 3 );
      Console.WriteLine( "[1,2,3]:   {0}", myArr3.GetValue( 1, 2, 3 ) );


      // Creates and initializes a seven-dimensional array.
      String[,,,,,,] myArr7 = new String[5,5,5,5,5,5,5];

      // Sets the element at index 1,2,3,0,1,2,3.
      int[] myIndices = new int[7] { 1, 2, 3, 0, 1, 2, 3 };
      myArr7.SetValue( "one-two-three-zero-one-two-three", myIndices );
      Console.WriteLine( "[1,2,3,0,1,2,3]:   {0}", myArr7.GetValue( myIndices ) );

   }
 
}


/* 
This code produces the following output.

[3]:   three
[1,3]:   one-three
[1,2,3]:   one-two-three
[1,2,3,0,1,2,3]:   one-two-three-zero-one-two-three

*/


[C++] 
#using <mscorlib.dll>
using namespace System;

int main()  {
 
      // Creates and initializes a one-dimensional array.
      String* myArr1[] = new String*[5];

      // Sets the element at index 3.
      myArr1->SetValue( S"three", 3 );
      Console::WriteLine( S"[3]:   {0}", myArr1->GetValue( 3 ) );


      // Creates and initializes a two-dimensional array.
      String* myArr2[,] = new String*[5,5];

      // Sets the element at index 1,3.
      myArr2->SetValue( S"one-three", 1, 3 );
      Console::WriteLine( S"[1,3]:   {0}", myArr2->GetValue( 1, 3 ) );


      // Creates and initializes a three-dimensional array.
      String* myArr3[,,] = new String*[5,5,5];

      // Sets the element at index 1,2,3.
      myArr3->SetValue( S"one-two-three", 1, 2, 3 );
      Console::WriteLine( S"[1,2,3]:   {0}", myArr3->GetValue( 1, 2, 3 ) );


      // Creates and initializes a seven-dimensional array.
      String* myArr7[,,,,,,] = new String*[5,5,5,5,5,5,5];

      // Sets the element at index 1,2,3,0,1,2,3.
      Int32 myIndices[] = { 1, 2, 3, 0, 1, 2, 3 };
      myArr7->SetValue( S"one-two-three-zero-one-two-three", myIndices);
      Console::WriteLine( S"[1,2,3,0,1,2,3]:   {0}", myArr7->GetValue( myIndices ) );

}


/* 
This code produces the following output.

[3]:   three
[1,3]:   one-three
[1,2,3]:   one-two-three
[1,2,3,0,1,2,3]:   one-two-three-zero-one-two-three

*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

Array Class | Array Members | System Namespace

Show:
© 2014 Microsoft