Thursday, January 3, 2008

Asp.net Basics

Variable Declarations

Dim x As Integer
Dim s As String
Dim s1, s2 As String
Dim o 'Implicitly Object
Dim obj As New Object()
Public name As String


Statements

Response.Write("foo")



Comments

' This is a comment

' This
' is
' a
' multiline
' comment


Accessing Indexed Properties

Dim s, value As String
s = Request.QueryString("Name")
value = Request.Cookies("Key").Value

'Note that default non-indexed properties
'must be explicitly named in VB



Declaring Indexed Properties

' Default Indexed Property
Public Default ReadOnly Property DefaultProperty(Name As String) As String
Get
Return CStr(lookuptable(Name))
End Get
End Property



Declaring Simple Properties

Public Property Name As String

Get
...
Return ...
End Get

Set
... = Value
End Set

End Property



Declare and Use an Enumeration

' Declare the Enumeration
Public Enum MessageSize

Small = 0
Medium = 1
Large = 2
End Enum

' Create a Field or Property
Public MsgSize As MessageSize

' Assign to the property using the Enumeration values
MsgSize = small



Enumerating a Collection

Dim S As String
For Each S In Coll
...
Next



Declare and Use Methods

' Declare a void return function
Sub VoidFunction()
...
End Sub

' Declare a function that returns a value
Function StringFunction() As String
...
Return CStr(val)
End Function

' Declare a function that takes and returns values
Function ParmFunction(a As String, b As String) As String
...
Return CStr(A & B)
End Function

' Use the Functions
VoidFunction()
Dim s1 As String = StringFunction()
Dim s2 As String = ParmFunction("Hello", "World!")

0 comments: