Print this page

Visual Basic.NET


VB.NET Quick Reference

Variable Declaration

Const trialDays As Integer  = 30 'Constant
Dim isInvoiced As BooleanTrue 'Boolean
Dim keystroke As Char '16  bit  Unicode character
Dim count As Integer  = 0 'Signed  32-bit integer
Dim invoiceId As Long  = 0 'Signed  64-bit integer
Dim total As Double  = 0 'Double  precision float
Dim tax As Single  = 0 'Single  precision float
Dim firstName As StringString.Empty 'String

 

Dim today As DateTime  =  DateTime.Now 'Current  Date Dim saleDate As  DateTime = New _

DateTime(2004,  1,  23,  13, 48,  39)

'(year,  month,  day,  hours, minutes,  seconds)

Dim items(100) As Integer 'Array  (See ArrayList)

Enum OrderStatus As Integer

NewOrder =  1

Picked

Shipped

End Enum

 

 

Procedures/Properties

//Access  Modifiers:  public, friend,
//private, protected,  protected  friend

Private Function Markup(ByVal total  _

As DoubleAs Double

Return  total  * _markupPercent

End Function

 

Friend Property InvoiceId() As Integer
   
Get

Return _invoiceId

End Get

Set(ByVal Value As Integer)

_invoiceId =  Value

End Set

End Property

 

Loops

'While  Loop

While  i  < 10

i  += 1

End While

 

'For  Loop

ForAs Integer  = 0 To 9

If (isFound) Then  _

Exit For 'Exit  loop

Next

'For  each  loop

Dim dt As DataTable

For Each _

dr As DataRow In dt.Rows

Console.WriteLine(  _

dr(0).ToString())

Next

'Do  Loop

Do

i  += 1

Loop Until  i  >  9

 

Branching

'If/Else  statement

If  (i >  0 AndAlso  j <>  5) OrElse  k  = 1 Then

Else  If (i  < 0) Then

End If

Select Case'Case statement

Case Is  < 2

Return  "Less  than two"

Case  3, 5

Return  "Three  or five"

Case Else 'Default

Return  "Other"

End Select

j= CInt(IIf(i  >  0, 1,  0))  'Immediate If

 

Operators

Computational

-  Negate/Subtract

*     Multiplication /      Division Float \     Division  Integer mod Modulus

+     Addition

Logical

not Logical  NOT

<      Less than

>      Greater than

<=   Less  than  or  equal >=   Greater  than  or

equal

=     Equality

<>   Inequality

And Logical  AND

Or   Logical  OR

AndAlso Short  Circuit
OrElse    Short  Circuit

Assignment

=    Assignment

Compound  assignment operators

+= Addition

-= Subtraction

*= Multiplication /= Division

 

Error Handling

Try

i  =  10 /  0

'  "Catch"  the error

Catch ex As Exception

DimAs String  = "Error:"  +  vbCrLf  _
 
+  "Target:  "  + ex.TargetSite.ToString  _
 
+  vbCrLf  +  "Error:  " _

+ ex.Message.ToString  +  vbCrLf _

+ "Trace:  "  +  ex.StackTrace.ToStringMsgBox(s, MsgBoxStyle.Critical,  _

"Error")

Finally 'Always  do  this

inputFile.Close()

End Try

 

Parameter Definitions

'Value,Reference,Dynamic Private Sub  Calc( _

ByValAs Integer, _

ByRefAs Integer, _

ByVal ar() As Integer)

End Sub

 


Previous page: Afx functions
Next page: Multi-threading in VB.NET