Array is a variable that store more than one value of same data type.Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the base class for all arrays in the common language runtime.
There is mainly three different kind of array
1) One dimensional array
2) Multi Dimensional array
Example for one dimensional array
Dim array1(4) as Integer
It holds five value with integer type.
array1(0)=1
array1(1)=2
array1(2)=3
array1(3)=4
array1(4)=5
Apart form the basic data type like integer, string ect… it also holds object like color
Example
Dim col(1) As Color
col(0) = Colors.Brown
col(1) = Colors.DarkGray
Reinitializing Array
Before we are initlize the array of specific type but later if we want to change its size we can alter using ReDim
Dim TestArray(5) As Integer
ReDim TestArray(15)
One drawback is once we reinitialize the array size we lose all the value in the array because every time it allocate new memory space.If you want to have the old data as before reinitialize then we have to use the Preserve keyword as below.
Dim TestArray() As Integer = {1, 3, 5}
ReDim Preserve TestArray(25)
GetLowerBound() and GetUpperBound() is used to get the first and last index of the array else we can use LBound(TestArray) and UBound(TestArray)
TestArray.GetLowerBound(0)
TestArray.GetUpperBound(0)
Multidimensional Array
To store more than one element of same or different data type in an array we are going to Multidimensional array. For example if we want to store Student name and his total in an array we can go for multidimensional array. Multi dimension means it may be two dimension or three dimension etc...
Some of example for multidimensional
Dim numbers(,) As Integer = {{1, 2}, {3, 4}, {5, 6}}Dim silings(,) As String = {{"Mike", "Amy"}, {"Mary", "Albert"}}
Console.WriteLine(number(0,1)) '20
Console.WriteLine(number(1,1)) 'Albert
Some more Examples
Public Class Customer
Public Id As Integer
Public Name As String
End Class
Now when you initialize this object in conventional VB.NET, this could be your approach,
Dim cust As New Customer()
With cust
.Id = 1
.Name = "VB.NET"
End With
Now in VB.NET 9.0 we do things in little differently,
Dim cust = New Customer() With {.Id = 2, .Name = "Kabilan"}
Also for array initialization in VB.NET we go for,
Dim objCusts(1) As Customer
objCusts(0) = New Customer() With {.Id = 3, .Name = "Kamal"}
objCusts(1) = New Customer() With {.Id = 4, .Name = "Ashok}
(or)
Dim objCusts() As Customer = { _
New Customer() With {.Id = 3, .Name = "Kamal"}, _
New Customer() With {.Id = 4, .Name = "Ashok"}}
}
Referrence Links
1) http://weblogs.asp.net/eporter/archive/2003/08/07/22943.aspx
2)http://msdn.microsoft.com/en-us/library/2yd9wwz4(VS.71).aspx
3)http://msdn.microsoft.com/en-us/library/system.array_members(VS.80).aspx
4)http://msdn.microsoft.com/en-us/library/0a7fscd0(VS.71).aspx
5) http://msdn.microsoft.com/en-us/library/2s05feca(VS.71).aspx
6)http://msdn.microsoft.com/en-us/library/szasx730(VS.71).aspx
Wednesday, April 22, 2009
Friday, March 27, 2009
Create a ASP.NET Dynamic Data Web Site Using Scaffolding
The basic requirement for Dynamic data web site:
1) Microsoft Visual Studio 2008 Service Pack 1 or Visual Web Developer 2008 Express Edition Service Pack 1.
2) Microsoft SQL Server 2005 or Microsoft SQL Server 2008.
Introduction
Using this Dynamic Data web site we can create data driven web application easily, using the LINQ to SQL or Entity Frame work data model. In the below example show using LINQ TO SQL. During the run time the Dynamic Data discover LINQ TO SQL and determining how to render UI from the data model. The Detailview, Formview, Gridview, Listview controls are used in this Dynamic data application to display the data, edit the data, and to delete the data.
Creating Dynamic Data Web Site
1) Open a project in VS2008 select the Dynamic Data Web Application, Click OK.
2) After creating the Solution, Right Click on the Solution Explore select
AddèNewItem
3) In the Add New Item Dialog Box select the LINQ to SQL Class template.
4) From the Server Explorer drag and drop the table from the SQL server.
5) Open the Global.asax and un coment the following line.
‘model.RegisterContext(GetType(YourDataContextType), New ContextConfiguration() With {.ScaffoldAllTables = False})
6) Change the ‘YourDataContextType’ with your DataContextClass Name in the above line. The DataContextClass name is the name of the LINQ to SQL Class template name followed by DataContext. You can find it in designer class page of LINQ to SQL Class file. Ather that set ScaffoldAllTables = True.
7) Now Build and run the page you see the cool Dynamic Data web site
Enjoy !
Friday, March 6, 2009
Populating GridView in ASP.NET
Basic Code for populating the Gridview in ASP.NET
Using C#
SqlConnection con = new SqlConnection("your connection string comeshere");
SqlDataAdapter da = new SqlDataAdapter("select firstname,lastname from employees", con); DataSet ds= new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
Using VB.NET
Dim con as SqlConnection =new SqlConnection ("your connection string comeshere")
dim da as SqlDataAdapter =new SqlDataAdapter ("select firstname,lastname from employees", con)
dim ds as new DataSet()
da.Fill(ds)
GridView1.DataSource = ds
GridView1.DataBind()
Using C#
SqlConnection con = new SqlConnection("your connection string comeshere");
SqlDataAdapter da = new SqlDataAdapter("select firstname,lastname from employees", con); DataSet ds= new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
Using VB.NET
Dim con as SqlConnection =new SqlConnection ("your connection string comeshere")
dim da as SqlDataAdapter =new SqlDataAdapter ("select firstname,lastname from employees", con)
dim ds as new DataSet()
da.Fill(ds)
GridView1.DataSource = ds
GridView1.DataBind()
Subscribe to:
Posts (Atom)