October 03, 2006

New Visual Basic Keywords

For those of you still jumping into VB.NET I have compiled a list of new keywords available in .NET!! These will come in handy so have a sharp pencil ready.

New Keywords:


  • Continue

  • Using

  • My

  • IsNot

  • TryCast



Below you will find quick explanations of each:

Continue: Enables you to work through loops more logically in specific situations.

Ex:

For i As Integer = 0 To (s.Length() - 1)
   If (s(i).Equals(" ")) Then Continue For
      spaceCounter += 1
Next


Using: The Using keyword ensures that resources get destroyed as soon as possible instead of remaining in memory until the method is executed. No more waiting for garbage collection! Note that there is no need to Dim our object.

Ex:

Using conn as New SqlConnection
   ' use the conn object
End Using ' Automatically disposes our conn object


My: This new keyword is designed to quickly give you access to a large variety of resources you may need to access when developing your applications. Different implementations in ASP.NET and VB.NET / Winforms.

Ex:

'Retrieve the computer name
Dim computername As String = My.Computer.Info.OSFullName


IsNot: The IsNot operator is the opposite of the Is operator.

Ex:
If myObject1 IsNot myObject2 Then ...


TryCast: The TryCast statement attempts to cast without having to handle the invalid cast exception. If the cast is invalid a Nothing value is returned.

Ex:
manager = TryCast(obj, Employee)
If manager IsNot Nothing Then
   ' use the manager object
End If


And that is that! Those are some extremely useful additions to the VB.NET language. Information was referenced from Professional ASP.NET 2.0, Appendix A. For further information and descriptions I highly recommend:

Professional ASP.NET 2.0
by Bill Evjen et al.
Wrox Press © 2006 (1290 pages)
ISBN:0764576100

kick it on DotNetKicks.com

2 comments:

Michael said...

VB.net is so horrible. Here are the keywords that I propose.

KindOf
If KindOf(x,y) then

SortOfButNotReally
If SortOfButNotReally(x,y) then

Almost
If Almost(x,y) then

KindOfResembles
If KindOfResembles(x,y) then

WasOnceEqualTo
If WasOnceEqualTo(x,y) then

Tony said...

Hmmm...How about:

BeginOverAgain
If (s(i).Equals(" ")) Then BeginOverAgain

IsMe
If IsMe(this) Then ...

IsIsNot
If IsIsNot(x, y) Then ...

IsNotIs
If IsNotIs(x, y) Then ...

TryAndNotBlow
If IsNotIs(x, y) Then
TryAndNotBlow
  'do something ...