asp.net.ph

Array.SetValue Method ( Object, Int32, Int32 )

System Namespace   Array Class


Sets a value to the element at the specified position in the two-dimensional Array.

[ VB ]
<Serializable>
Overloads Public Sub SetValue ( _
   ByVal value As Object, _
   ByVal index1 As Integer, _
   ByVal index2 As Integer _
)

[ C# ]
[Serializable]
public void SetValue (
   object value,
   int index1,
   int index2
);

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

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

Parameters

value
The new value for the specified element.
index1
The first-dimension index of the Array element to set.
index2
The second-dimension index of the Array element to set.

Exceptions


Exception Type Condition
ArgumentException The current Array does not have exactly two dimensions.

-or-

value does not contain a type that can be widened, using the standard widening conversions, to the element type of the current Array.

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.

For more information about conversions, see Convert.

Example

The following code example shows how to use SetValue to initialize an 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.SetValue Overload List   GetLowerBound   GetUpperBound   GetValue 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