Thursday, January 3, 2008

Asp.net Basics-3

String Concatenation

' Using Strings
Dim s1, s2 As String
s2 = "hello"
s2 &= " world"
s1 = s2 & " !!!"

' Using StringBuilder class for performance
Dim s3 As New StringBuilder()
s3.Append("hello")
s3.Append(" world")
s3.Append(" !!!")


Event Handler Delegates

Sub MyButton_Click(Sender As Object,
E As EventArgs)
...
End Sub



Declare Events

' Create a public event
Public Event MyEvent(Sender as Object, E as EventArgs)

' Create a method for firing the event
Protected Sub OnMyEvent(E As EventArgs)
RaiseEvent MyEvent(Me, E)
End Sub


Add or Remove Event Handlers to Events

AddHandler Control.Change, AddressOf Me.ChangeEventHandler
RemoveHandler Control.Change, AddressOf Me.ChangeEventHandler


Casting

Dim obj As MyObject
Dim iObj As IMyObject
obj = Session("Some Value")
iObj = CType(obj, IMyObject)




Conversion

Dim i As Integer
Dim s As String
Dim d As Double

i = 3
s = i.ToString()
d = CDbl(s)

' See also CDbl(...), CStr(...), ...



Class Definition with Inheritance

Imports System

Namespace MySpace

Public Class Foo : Inherits Bar

Dim x As Integer

Public Sub New()
MyBase.New()
x = 4
End Sub

Public Sub Add(x As Integer)
Me.x = Me.x + x
End Sub

Overrides Public Function GetNum() As Integer
Return x
End Function

End Class

End Namespace

' vbc /out:libraryvb.dll /t:library
' library.vb



Implementing an Interface

Public Class MyClass : Implements IEnumerable
...

Function IEnumerable_GetEnumerator() As IEnumerator _
Implements IEnumerable.GetEnumerator
...
End Function
End Class

Class Definition with a Main Method

Imports System

Public Class ConsoleVB

Public Sub New()
MyBase.New()
Console.WriteLine("Object Created")
End Sub

Public Shared Sub Main()
Console.WriteLine("Hello World")
Dim cvb As New ConsoleVB
End Sub

End Class

' vbc /out:consolevb.exe /t:exe console.vb



Standard Module

Imports System

Public Module ConsoleVB

Public Sub Main()
Console.WriteLine("Hello World")
End Sub

End Module

' vbc /out:consolevb.exe /t:exe console.vb

0 comments: