Performing Loops – VB and C#

This will be a short post. I just wanted to display the differences between C# and VB when performing loops.  See below:

VB:

For i As Integer = 10 To 1 Step -1

Console.WriteLine(i)

If i < 5 Then

Exit For

End If

Next

 

Dim start As Integer = 5

For i As Integer = start To start + 10

Console.WriteLine(i)

Next

 

Dim str As String

Do

str = Console.ReadLine()

Console.WriteLine(str)

Loop Until str = “”

 

Do

str = Console.ReadLine().Trim()

If String.IsNullOrEmpty(str) Then

Exit Do

End If

Console.WriteLine(“You typed:{0}”, str)

Loop

Dim names As String() = New String() {“Bob”, “David”}

For Each name As String In names

Console.WriteLine(name)

Next

 

C#: 

for (int i=0; i<10; i++) {}

 

while (i<10) { i++; }

 

do { i++; } while (i<10);

 

foreach (ListItem item in list.Items) {}

 

Searching for Files Using ASP.NET VB

      One of the problems I had finding is searching directories for files via user input. Right now I am using a text field to allow users to enter search criteria. I may go to drop downs. The reason I may do this is because the files I am uploading are all being renamed as per the directory structure. (i.e. test.txt would be renamed to asp_ dept_station_1 with the directory structure being c:/asp/dept/station/). Then I just tack on a consecutive number at the end. But I digress. What I have found to be a good way of searching all files in the folders is to loop though all directories and add the files to an array() of type fileinfo. After that, I then loop the array and add the found files to a datalist. I have found this to work pretty well. I am now working on getting the search to allow for more than one word. That is why I may just go to dropdowns. Make sense? Anyway, the code is below for what I am doing currently. I will re-post if I decide to change it. If there are any improvements that can be made, please let me know. Thank you. Now that I think about it, I am using the same code as the post: Recursively Browse Directories Using ASP.NET VB I only added an ‘if’ loop to check for search criteria in filename:

If fi.Name.Contains(searchCriteria.ToUpper) Then
filelink = " Server.UrlEncode(dirname) + directorySeparatorChar + Server.UrlEncode(fi.Name) + "&action=download>" + fi.Name + "
"
row = myfiledt.NewRow()
row("filename") = filelink
row("creationtime") = fi.CreationTime
myfiledt.Rows.Add(row)
End If
position += 1 …

Working With Numbers in VB.NET

Dim i As Integer = 0

i += 10 ‘ add 10 to i

i -= 5 ‘ subtract 5 from i

i *= 2 ‘ multiply i by 2

i /= 2 ‘ divide i by 2

‘ parse strings as numbers

‘ all number types have Parse and TryParse shared functions

Try

i = Integer.Parse(Console.ReadLine())

Console.WriteLine(“You typed a number”)

Catch ex As FormatException

Console.WriteLine(“Please type a number”)

End Try

If Integer.TryParse(Console.ReadLine(), i) Then

Console.WriteLine(“You typed a number”)

Else

Console.WriteLine(“Please type a number”)

End If

‘ money

Dim price As Decimal = 19.99

price = Decimal.Parse(“$39.95″ NumberStyles.Currency)