Array get value
The following asp.net c# example code demonstrate us how can we get a
specified element value froman array object programmatically at run time
in an asp.net application. .Net framework's Array Class Array.GetValue()method
get the value of the specified element in the current array.
Array.GetValue(Int32) overloaded method allow us to get the value at the specified position in the one-dimensional array.In this example, we uses this overloaded member. GetValue(Int32) method has a required parameter named 'index' which valuedata type is System.Int32. This parameter value contains a 32-bit integer that represent the position of the array elementto get.
Array.GetValue(Int32) method return a System.Object type value. Method return the element value at the specified positionin the one dimensional array.
So, to get a specified element value of an one-dimensional array by index number, we can call the GetValue(Int32) methodas this way Array.GetValue(ElementIndex). Finally, we get the specified element value.
Array.GetValue(Int32) overloaded method allow us to get the value at the specified position in the one-dimensional array.In this example, we uses this overloaded member. GetValue(Int32) method has a required parameter named 'index' which valuedata type is System.Int32. This parameter value contains a 32-bit integer that represent the position of the array elementto get.
Array.GetValue(Int32) method return a System.Object type value. Method return the element value at the specified positionin the one dimensional array.
So, to get a specified element value of an one-dimensional array by index number, we can call the GetValue(Int32) methodas this way Array.GetValue(ElementIndex). Finally, we get the specified element value.
array-getvalue.aspx
<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
string[] birds = new string[]
{
"Indian Robin",
"Northern Wheatear",
"Bluethroat",
"Spotted Flycatcher",
"Thrush Nightingale"
};
Label1.Text = "birds array.........<br />";
foreach(string s in birds)
{
Label1.Text += s + "<br />";
}
string index1bird = birds.GetValue(1).ToString();
string index3bird = birds.GetValue(3).ToString();
Label1.Text += "<br />birds array index 1 bird is: " + index1bird;
Label1.Text += "<br />birds array index 3 bird is: " + index3bird;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>c# example - array getvalue</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:DarkBlue; font-style:italic;">
c# example - array getvalue
</h2>
<hr width="550" align="left" color="LightBlue" />
<asp:Label
ID="Label1"
runat="server"
Font-Size="Large"
>
</asp:Label>
<br /><br />
<asp:Button
ID="Button1"
runat="server"
Text="array getvalue"
OnClick="Button1_Click"
Height="40"
Font-Bold="true"
/>
</div>
</form>
</body>
</html>