Wednesday, March 5, 2008

How to use Operators of VB?

_____________________________________________

How to use Operators in VB?

_____________________________________________

Comparison
= < > <= >= <>

Arithmetic
+ - * /
Mod
\ (integer division)
^ (raise to a power)

Assignment
= += -= *= /= \= ^= <<= >>= &=

Bitwise
And Or Xor Not << >>

Logical
AndAlso OrElse And Or Xor Not

Note: AndAlso and OrElse perform short-circuit logical evaluations

String Concatenation
&

____________________________________________________________________

Note:Applicable for .net also asp.net.
____________________________________________________________________

How to use Loops of VB?

_____________________________________________

How to use Loops of VB?

_____________________________________________

Pre-test Loops:
While c <>End While

Do Until c = 10
c += 1
Loop

Do While c <>Loop

For c = 2 To 10 Step 2
Console.WriteLine(c)
Next


Post-test Loops:
Do
c += 1
Loop While c <>
Do
c += 1
Loop Until c = 10

' Array or collection looping
Dim names As String() = {"Fred", "Sue", "Barney"}
For Each s As String In names
Console.WriteLine(s)
Next

' Breaking out of loops
Dim i As Integer = 0
While (True)
If (i = 5) Then Exit While
i += 1
End While


' Continue to next iteration
For i = 0 To 4
If i <>Continue For
Console.WriteLine(i) ' Only prints 4
Next

____________________________________________________________________

Note:Applicable for .net also asp.net.
____________________________________________________________________

How to use IF Statement of VB?

_____________________________________________

How to use IF Statement of VB?

_____________________________________________

greeting = IIf(age <>

' One line doesn't require "End If"
If age <>Then greeting = "What's up?"
If age <>Then greeting = "What's up?" Else greeting = "Hello"

' Use : to put two commands on same line
If x <> 100 And y <>Then x *= 5 : y *= 2

' Preferred
If x <> 100 And y <>Then
x *= 5
y *= 2
End If

' To break up any long single line use _
If whenYouHaveAReally < longLine And _
itNeedsToBeBrokenInto2 > Lines Then _
UseTheUnderscore(charToBreakItUp)

'If x > 5 Then
x *= y
ElseIf x = 5 Then
x += y
ElseIf x <>Then
x -= y
Else
x /= y
End If

Select Case color ' Must be a primitive data type
Case "pink", "red"
r += 1
Case "blue"
b += 1
Case "green"
g += 1
Case Else
other += 1
End Select

____________________________________________________________________

Note:Applicable for .net also asp.net.
____________________________________________________________________

Namespaces of VB.

_____________________________________________

Namespaces of VB.

_____________________________________________

Namespace Harding.Compsci.Graphics
...
End Namespace

' or

Namespace Harding
Namespace Compsci
Namespace Graphics
...
End Namespace
End Namespace
End Namespace

Imports Harding.Compsci.Graphics

____________________________________________________________________

Note:Applicable for .net also asp.net.
____________________________________________________________________

File I/O of VB.

_____________________________________________

File I/O of VB.

_____________________________________________

Imports System.IO

' Write out to text file
Dim writer As StreamWriter = File.CreateText("c:\myfile.txt")
writer.WriteLine("Out to file.")
writer.Close()

' Read all lines from text file
Dim reader As StreamReader = File.OpenText("c:\myfile.txt")
Dim line As String = reader.ReadLine()
While Not line Is Nothing
Console.WriteLine(line)
line = reader.ReadLine()
End While
reader.Close()

' Write out to binary file
Dim str As String = "Text data"
Dim num As Integer = 123
Dim binWriter As New BinaryWriter(File.OpenWrite("c:\myfile.dat"))
binWriter.Write(str)
binWriter.Write(num)
binWriter.Close()

' Read from binary file
Dim binReader As New BinaryReader(File.OpenRead("c:\myfile.dat"))
str = binReader.ReadString()
num = binReader.ReadInt32()
binReader.Close()

____________________________________________________________________

Note:Applicable for .net also asp.net.
____________________________________________________________________

Constructors / Destructors of VB.

_____________________________________________

Constructors / Destructors of VB.

_____________________________________________

Class SuperHero
Private _powerLevel As Integer

Public Sub New()
_powerLevel = 0
End Sub

Public Sub New(ByVal powerLevel As Integer)
Me._powerLevel = powerLevel
End Sub

Protected Overrides Sub Finalize()
' Desctructor code to free unmanaged resources
MyBase.Finalize()
End Sub
End Class

____________________________________________________________________

Note:Applicable for .net also asp.net.
____________________________________________________________________

Program Structure of VB.

_____________________________________________

Program Structure of VB.

_____________________________________________

Imports System

Namespace Hello
Class HelloWorld
Overloads Shared Sub Main(ByVal args() As String)
Dim name As String = "VB.NET"

'See if an argument was passed from the command line
If args.Length = 1 Then name = args(0)

Console.WriteLine("Hello, " & name & "!")
End Sub
End Class
End Namespace

____________________________________________________________________

Note:Applicable for .net also asp.net.
____________________________________________________________________