Sample Programs in VB

Sample Programs in VB

Program to check the range of a datatype

Public Class Max_Min_Values
Public Shared Sub Main()
Console.WriteLine(Integer.MaxValue)
Console.WriteLine(Integer.MinValue)
Console.WriteLine(Double.MaxValue)
Console.WriteLine(Double.MinValue)
End Sub
End Class

Prog to demonstrate In-Built AscW and ChrW Functions:

Class GetAscii
Shared Sub Main()
Dim str As String = Console.ReadLine()
Dim ch As Char = CChar(str)
Dim n As Integer = AscW(ch)
Console.WriteLine(n)
End Sub
End Class

Program to demonstrate If Statement

Public Class IfDemo
Shared Sub Main()
Dim a, b, c As Integer
Console.WriteLine("Enter 3 Numbers")
a = Integer.Parse(Console.ReadLine())
b = Integer.Parse(Console.ReadLine())
c = Integer.Parse(Console.ReadLine())
Dim max As Integer = a
If (b > max) Then max = b
If (c > max) Then max = c
Console.WriteLine("Greatest No is " & max)
End Sub
End Class

Class IfDemo2

Public Shared Sub Main(ByVal args() As String)
If (args.Length = 0) Then
Console.WriteLine("Please provide a number")
ElseIf (Not Integer.TryParse(args(0), 0)) Then
Console.WriteLine("Not a number")
ElseIf (Integer.Parse(args(0)) Mod 2 = 0) Then
Console.WriteLine("Even number")
Else
Console.WriteLine("Odd number")
End If
End Sub
End Class

Program to demonstrate Select statement

Public Class SelectDemo
Shared Sub Main()
Dim grade As String
Console.WriteLine("Enter a grade")
grade = Console.ReadLine()
Select Case (grade)
Case "E" , "e"
Console.WriteLine("Excellent")
Case "G" , "g"
Console.WriteLine("Good")
Case Else
Console.WriteLine("No Such Grades")
End Select
End Sub
End Class
Public Class SelectCases
Shared Sub Main()
Dim n As Integer
Console.WriteLine("Enter a Number")
n = Integer.Parse(Console.ReadLine())
Select Case (n)
Case 1
Console.WriteLine("Case 1")
Case 2, 3
Console.WriteLine("Case 2,3")
Case 4 To 7
Console.WriteLine("Case 4 To 7")
Case Is < 9
Console.WriteLine("Case Is < 9") Case Is > 9
Console.WriteLine("Case Is > 9")
End Select
End Sub
End Class

Program to demonstrate While loop

Public Class WhileDemo
Shared Sub Main()
Dim marks, count, total As Integer
Dim avg as Double
Console.WriteLine("Enter 6 Subject marks")
While (count < 6)
marks = Integer.Parse(Console.ReadLine())
total += marks
count += 1
End While
avg = total / 6
Console.WriteLine("Total is " & total)
Console.WriteLine("Average is " & avg)
End Sub
End Class

Program to demonstrate While loop

Public Class WhileDemo
Shared Sub Main()
Dim marks, count, total As Integer
Dim avg as Double
Console.WriteLine("Enter 6 Subject marks")
While (count < 6)
marks = Integer.Parse(Console.ReadLine())
total += marks
count += 1
End While
avg = total / 6
Console.WriteLine("Total is " & total)
Console.WriteLine("Average is " & avg)
End Sub
End Class

Program to generate table using For loop

Class Table
Shared Sub Main()
Dim n As Integer
Console.Write("Table of: ")
n = Integer.Parse(Console.ReadLine())
Dim s As String = ""
For i As Integer = 1 To 10
s &= n & "*" & i & "=" & n * i & vbCrLf
Next
Console.WriteLine(s)
End Sub
End Class

Program to print a range of numbers

Dim m, n, incr As Integer
Console.Write("Enter Starting Number: ")
m = Integer.Parse(Console.ReadLine())
Console.Write("Enter Ending Number: ")
n = Integer.Parse(Console.ReadLine())
Console.Write("Increment By: ")
incr = Integer.Parse(Console.ReadLine())
For i As Integer = m To n Step incr
Console.WriteLine(i)
Next i

Program to generate a triangle using For loop

Class Triangle
Public Shared Sub Main()
For i As Integer = 1 To 10
For j As Integer = 1 To i
Console.Write("*" & " ")
Next
Console.WriteLine()
Next
Dim n, m, o As Integer
For n = 10 To 1 Step -1
For o = 0 To n - 1
Console.Write(" ")
Next
For m = n To 10
Console.Write("* ")
Next
Console.WriteLine()
Next
End Sub
End Class

Class PalindromeProgram
Shared Sub Main(ByVal args As String())
Dim str As String
str = Console.ReadLine()
Dim n As Integer = str.Length
For i As Integer = 0 To n \ 2
If (str(i) <> str(n – i – 1)) Then
Console.WriteLine(“Its not a palindrome”)
Return
End If
Next
Console.WriteLine(“Its a palindrome”)
End Sub
End Class

Program to demonstrate Arrays

Public Class Array_Example
Public Shared Sub Main()
Dim ar() As Integer = {1, 2, 3, 4, 5}
Console.WriteLine(ar.Length)
For Each tmp As Integer In ar
Console.WriteLine(tmp)
Next
ReDim ar(10)
For Each tmp As Integer In ar
Console.WriteLine(tmp)
Next
End Sub
End Class

Program to demonstrate Arrays and Array class

Class Program
Shared Sub Main()
Dim ar(-1) As Integer
While (True)
Select Case GetOption()
Case 1
ar = CreateArray()
Case 2
Array.Sort(ar)
Case 3
Array.Reverse(ar)
Case 4
PrintArray(ar)
Case 5
Console.WriteLine("Thank you")
Return
Case Else
Console.WriteLine("Invalid option")
End Select
End While
End Sub
Shared Function GetOption() As Integer
Console.WriteLine("1. Create")
Console.WriteLine("2. Sort")
Console.WriteLine("3. Reverse")
Console.WriteLine("4. Print")
Console.WriteLine("5. Exit")
Console.Write("Enter your option: ")
Return Integer.Parse(Console.ReadLine())
End Function
Shared Function CreateArray() As Integer()
Console.Write("Enter the array length: ")
Dim n As Integer
n = Integer.Parse(Console.ReadLine())
Dim ar(n - 1) As Integer
For i As Integer = 0 To n - 1
Console.Write("Enter the " & i & "th value: ")
ar(i) = CInt(Console.ReadLine())
Next
Return ar
End Function
Shared Sub PrintArray(ByVal ar As Integer())
For Each value As Integer In ar
Console.Write(value & " ")
Next
Console.WriteLine()
End Sub
End Class

Multithreading in C Sharp Part1

Multithreading in C# – Threading Overview & Scheduling

Overview of Process and Threads

Process:

  • A process is a running instance of an application.
  • Every process has its own memory address space (execution environment) and it is in that all the data and
    the instructions of that process reside.
  • A process has a self-contained execution environment. A process generally has a complete, private set of
    basic run-time resources; in particular, each process has its own memory space

Thread:

  • The path of execution of instructions within a process is called a thread.
  • It has a starting point, execution sequence and terminating point.

A program with multiple simultaneous paths of execution is said to be multithreaded application.

Multithreading Multiprocessing/Multitasking
Multithreading is the ability of an operating system to execute the different parts of the program,
called threads, concurrently.
Multitasking is the ability of an operating system to accommodate more than one program in the memory at
the same time.
In multithreading all threads share same process address space [memory]. Processes do not share the memory
Threads can share variables since threads are in same process. External resources like file system or database must be used for communication between multiple processes and
each one runs under its unique process address space.
Inter thread communication is simple Inter process communication is complex
Less resources required to launch/run them since they are part of an already running process, hence they are
called as light weight tasks.
CPU has to spend more resources for launching a new process hence process is known as heavy weight.

Types of Scheduling

Preemptive: OS can un-schedule a thread even if it is not completed its task.

Non-Preemptive: OS cannot unschedule the thread. A Thread should either get unscheduled itself or it should
complete its task and release the processor.

  • Scheduling is done based on the priority of the thread. A thread with highest priority is executed in
    the processor. If multiple threads exist with same priority then a time slice is assigned to each thread.
  • In MS-Windows execution of the thread is based on: Preemptive Scheduling –> Priority –> Time slice
  • Also, in all versions of MS Windows, to avoid starvation low priority threads are also executed in the processor
    but the number of times they execute is very less.
  • Switching means that the processor stores the state of the outgoing thread (it does so by noting the current
    processor register values and the last instruction-set the thread was about to perform), restores the state of the
    incoming thread (again by restoring its processor register values and picking the last instruction-set where it had
    left itself) and then runs it.

Note: On a multi-processor system, the operating system can allocate individual threads to the separate
processors, which thus fastens the execution of the program. The efficiency of the threads also increases
significantly because the distribution of the threads on several processors is faster than sharing time-slices
on a single processor. It is particularly useful to have a multi-processor system for 3D modeling and image-processing.

Example of some Multithreading Applications:

  1. MS-Word – Auto Saving, Spell Checking, Printing while editing
  2. VS.NET – Intellisense, Auto Compilation.
  3. Browser – Downloading multiple files and browsing simultaneously.
  4. Media Player – Movie and Sound

For Complete C# Online training with material

Introducing Collections in C#

Introducing Collections

  • Closely related data can be handled more efficiently when grouped together into a collection. Instead of writing separate code to handle each individual object, you can use the same code to process all the elements of a collection.
  • A collection is a set of similarly typed objects that are grouped together. A collection is an object by itself, which
  • would manage other objects eg: ArrayList, HashTable, Stack, Queue, LinkedList, SortedList etc.
  • Collection classes are defined as part of the System.Collections and ystem.Collections.Generic namespace.
  • Most collection classes derive from the interfaces ICollection, IComparer, IEnumerable, IList, IDictionary, and IDictionaryEnumerator and their generic equivalents.
Benefits of Collections classes:
 
1. Built-in Sorting capabilities,
2. Most of them are automatically indexed to give best performance.
3. Memory management is handled automatically and Capacity of a collection is expanded as required.
4. Synchronization provides thread safety when accessing members of the collection.
5. Collections classes can generate wrappers that make the collection read-only or fixed-size.
6. Collections class can generate its own enumerator that makes it easy to iterate through the elements.
System.Collections classes can generally be categorized into three types:
  • Commonly used collections.
These are the common variations of data collections, such as hash tables, queues, stacks, dictionaries, and lists.
  • Bit collections.
These are collections whose elements are bit flags. They behave slightly differently from other collections.
  • Specialized collections.
These are collections with highly specific purposes, usually to handle a specific type of element, such as
NameValueCollection, StringDictionary and StringCollection
Closely related data can be handled more efficiently when grouped together into a collection. Instead of writing
separate code to handle each individual object, you can use the same code to process all the elements of a
collection.
i ) A collection is a set of similarly typed objects that are grouped together. A collection is an object by itself, which
would manage other objects eg: ArrayList, HashTable, Stack, Queue, LinkedList, SortedList etc.
ii ) Collection classes are defined as part of the System.Collections and System.Collections.Generic namespace.
iii ) Most collection classes derive from the interfaces ICollection, IComparer, IEnumerable, IList, IDictionary, and
IDictionaryEnumerator and their generic equivalents.
Benefits of Collections classes
1. Built-in Sorting capabilities,
2. Most of them are automatically indexed to give best performance.
3. Memory management is handled automatically and Capacity of a collection is expanded as required.
4. Synchronization provides thread safety when accessing members of the collection.
5. Collections classes can generate wrappers that make the collection read-only or fixed-size.
6. Collections class can generate its own enumerator that makes it easy to iterate through the elements.
System.Collections classes can generally be categorized into three types:
  • Commonly used collections.
These are the common variations of data collections, such as hash tables, queues, stacks, dictionaries, and
lists.
  • Bit collections.
These are collections whose elements are bit flags. They behave slightly differently from other collections.
  • Specialized collections.
These are collections with highly specific purposes, usually to handle a specific type of element, such as

NameValueCollection, StringDictionary and StringCollection

 

Understanding-and-Using-Commonly-Used-Collections
Learn Online C# Training for Beginners to Advanced