asp.net.ph

Array.GetValue Method ( Int32, Int32 )

System Namespace   Array Class


Returns the value at the specified position in the two-dimensional Array.

[ VB ]
<Serializable>
Overloads Public Function GetValue ( _
   ByVal index1 As Integer, _
   ByVal index2 As Integer _
) As Object

[ C# ]
[Serializable]
public object GetValue (
   int index1,
   int index2
);

[ C++ ]
[Serializable]
public: Object* GetValue (
   int index1,
   int index2
);

[JScript ]
public Serializable
function GetValue (
   index1 : int,
   index2 : int
) : Object;

Parameters

index1
The first-dimension index of the Array element to get.
index2
The second-dimension index of the Array element to get.

Return Value

The value at the specified position in the two-dimensional Array.

Exceptions


Exception Type Condition
ArgumentException The current Array does not have exactly two dimensions.
IndexOutOfRangeException Either index1 or index2 is outside the range of valid indexes for the corresponding dimension of the current Array.

Remarks

The GetLowerBound and GetUpperBound methods can determine whether any of the indexes is out of bounds.

Example

The following code example shows how to use GetValue to get a specific value in the Array.

[ VB ] 
Imports System
Imports Microsoft.VisualBasic

Public Class SamplesArray    
    
    Public Shared Sub Main ( ) 
        
        ' Creates and initializes a new Array.
        Dim myArray As Array = Array.CreateInstance ( GetType ( String ) , 2, 4 ) 
        myArray.SetValue ( "The", 0, 0 ) 
        myArray.SetValue ( "quick", 0, 1 ) 
        myArray.SetValue ( "brown", 0, 2 ) 
        myArray.SetValue ( "fox", 0, 3 ) 
        myArray.SetValue ( "jumped", 1, 0 ) 
        myArray.SetValue ( "over", 1, 1 ) 
        myArray.SetValue ( "the", 1, 2 ) 
        myArray.SetValue ( "lazy", 1, 3 ) 
        
        ' Displays the values of the Array.
        Response.WriteLine ( "The Array contains the following values:" ) 
        Dim i, j As Integer
        For i = myArray.GetLowerBound ( 0 ) To myArray.GetUpperBound ( 0 ) 
            For j = myArray.GetLowerBound ( 1 ) To myArray.GetUpperBound ( 1 ) 
                Response.WriteLine ( ControlChars.Tab + "[{0},{1}]:" _
                   + ControlChars.Tab + "{2}", i, j, myArray.GetValue ( i, j ) ) 
            Next j
        Next i
    End Sub    
End Class

' This code produces the following output.
' 
' The Array contains the following values:
'     [0,0]:    The
'     [0,1]:    quick
'     [0,2]:    brown
'     [0,3]:    fox
'     [1,0]:    jumped
'     [1,1]:    over
'     [1,2]:    the
'     [1,3]:    lazy

[ C# ] 
using System;
public class SamplesArray  {

   public static void Main ( )  {

      // Creates and initializes a new Array.
      Array myArray=Array.CreateInstance ( typeof ( String ) , 2, 4 );
      myArray.SetValue ( "The", 0, 0 );
      myArray.SetValue ( "quick", 0, 1 );
      myArray.SetValue ( "brown", 0, 2 );
      myArray.SetValue ( "fox", 0, 3 );
      myArray.SetValue ( "jumped", 1, 0 );
      myArray.SetValue ( "over", 1, 1 );
      myArray.SetValue ( "the", 1, 2 );
      myArray.SetValue ( "lazy", 1, 3 );

      // Displays the values of the Array.
      Response.WriteLine ( "The Array contains the following values:" );
      for ( int i = myArray.GetLowerBound ( 0 ); i <= myArray.GetUpperBound ( 0 ); i++ ) 
         for ( int j = myArray.GetLowerBound ( 1 ); j <= myArray.GetUpperBound ( 1 ); j++ ) 
            Response.WriteLine ( "\t[{0},{1}]:\t{2}", i, j, myArray.GetValue ( i, j ) );
   }
}
/* 
This code produces the following output.

The Array contains the following values:
    [0,0]:    The
    [0,1]:    quick
    [0,2]:    brown
    [0,3]:    fox
    [1,0]:    jumped
    [1,1]:    over
    [1,2]:    the
    [1,3]:    lazy
*/ 

[JScript ] 
import System;

// Creates and initializes a new Array.
var myArray : System.Array = System.Array.CreateInstance ( System.String, 2, 4 );
myArray.SetValue ( "The", 0, 0 );
myArray.SetValue ( "quick", 0, 1 );
myArray.SetValue ( "brown", 0, 2 );
myArray.SetValue ( "fox", 0, 3 );
myArray.SetValue ( "jumped", 1, 0 );
myArray.SetValue ( "over", 1, 1 );
myArray.SetValue ( "the", 1, 2 );
myArray.SetValue ( "lazy", 1, 3 );

// Displays the values of the Array.
Response.WriteLine ( "The Array contains the following values:" );
for ( var i : int = myArray.GetLowerBound ( 0 ); i <= myArray.GetUpperBound ( 0 ); i++ ) 
  for ( var j : int = myArray.GetLowerBound ( 1 ); j <= myArray.GetUpperBound ( 1 ); j++ ) 
     Response.WriteLine ( "\t[{0},{1}]:\t{2}", i, j, myArray.GetValue ( i, j ) );
 
 /* 
 This code produces the following output.
 
 The Array contains the following values:
     [0,0]:    The
     [0,1]:    quick
     [0,2]:    brown
     [0,3]:    fox
     [1,0]:    jumped
     [1,1]:    over
     [1,2]:    the
     [1,3]:    lazy
 */ 
See Also

Array Members   Array.GetValue Overload List   GetLowerBound   GetUpperBound   SetValue Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph