Programming in ASP.net
File Handling in VB.net
The FileStream Class
The FileStream class in the System.IO namespace helps in reading from, writing to and closing files. This class derives from the abstract class Stream.
You need to create a FileStream object to create a new file or open an existing file. The syntax for creating a FileStream object is as follows:
Dim As FileStream = New FileStream(, , , )
For example, for creating a FileStream object F for reading a file named sample.txt:
Dim f1 As FileStream = New FileStream(“test.dat”, FileMode.OpenOrCreate, FileAccess.ReadWrite)
Parameter Description
FileMode The FileMode enumerator defines various methods for opening files. The members of the FileMode enumerator are:
• Append: It opens an existing file and puts cursor at the end of file, or creates the file, if the file does not exist.
• Create: It creates a new file.
• CreateNew: It specifies to the operating system that it should create a new file.
• Open: It opens an existing file.
• OpenOrCreate: It specifies to the operating system that it should open a file if it exists, otherwise it should create a new file.
• Truncate: It opens an existing file and truncates its size to zero bytes.
FileAccess FileAccess enumerators have members: Read, ReadWrite and Write.
FileShare FileShare enumerators have the following members:
• Inheritable: It allows a file handle to pass inheritance to the child processes
• None: It declines sharing of the current file
• Read: It allows opening the file for reading
• ReadWrite: It allows opening the file for reading and writing
• Write: It allows opening the file for writing
Posted By-: Vissicomp Technology Pvt. Ltd.
Website -: http://www.vissicomp.com
COLLECTIONS
COLLECTIONS
An array stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.
Creating Arrays in VB.Net
To declare an array in VB.Net, you use the Dim statement. For example –
Dim intData(30) ' an array of 31 elements Dim strData(20) As String ' an array of 21 strings Dim twoDarray(10, 20) As Integer 'a two dimensional array of integers Dim ranges (10, 100) 'a two dimensional array
You can also initialize the array elements while declaring the array For example,
Dim intData() As Integer = {12, 16, 20, 24, 28, 32} Dim names () As String = {"Karthik", "Sandhya", "Shivangi", "Ashwitha", "Somnath"} Dim miscData() As Object = {"Hello World", 12d, 16ui, "A"c}
The elements in an array can be stored and accessed by using the index of the array. The following program demonstrates this:
Module arrayApl Sub Main() Dim n(10) As Integer ' n is an array of 11 integers ' Dim i, j As Integer ' initialize elements of array n ' For i = 0 To 10 n(i) = i + 100 ' set element at location i to i + 100 Next i ‘output each array element's value ' For j = 0 To 10 Console.WriteLine("Element({0}) = {1}", j, n(j)) Next j Console.ReadKey () End Sub End Module
When the above code is compiled and executed, it produces following result:
Element(0) = 100 Element(1) = 101 Element(2) = 102 Element(3) = 103 Element(4) = 104 Element(5) = 105 Element(6) = 106 Element(7) = 107 Element(8) = 108 Element(9) = 109 Element(10) = 110 Posted by vissicomp Technology pvt ltd www.vissicomp.com
FOREACH LOOP IN ASP.NET
FOREACH LOOP
- A foreach loop is used to iterate through the items in a list. It operates on arrays or collections such as ArrayList, which can be found in the System.Collections namespace. The syntax of a foreach loop is foreach (<type> <iteration variable> in <list>) {<statements>}.
- The type is the type of item contained in the list. For example, if the type of the list was int[] then the type would be int.
- The iteration variable is an identifier that you choose, which could be anything but should be meaningful. For example, if the list contained an array of people’s ages, then a meaningful name for item name would be age.
- The in keyword is required.
- While iterating through the items of a list with a foreach loop, the list is read-only. This means that you can’t modify the iteration variable within a foreach loop. On each iteration through a foreach loop the list is queried for a new value.
- As long as the list can return a value, this value will be put into the read-only iteration variable, causing the statements in the foreach block to be executed. When the collection has been fully traversed, control will transfer to the first executable statement following the end of the foreach block.
class ForEachLoop
{ public static void Main()
{
string[] names = {“Cheryl”, “Joe”, “Matt”, “Robert”};
foreach (string person in names)
{
Console.WriteLine(“{0} “, person);
}
}
}
Output
- In the foreach loop, we’ve used a string variable, person, as the item name, to hold each element of the names array. As long as there are names in the array that have not been returned, the Console.WriteLine method will print each value of the person variable to the screen.
Posted by Vissicomp Technology Pvt Ltd
For more http://www.vissicomp.com
You must be logged in to post a comment.