Saturday, August 21, 2010

VB.NET - Basics by Example



continue with the Basics by Example; today's version of the post written in VB.NET Enjoy!

You can copy and paste the code below in your favorite IDE/Editor and start playing and learning with it. This little "working" program will teach you the basics of the Programming Language.

There are some "comments" on the code added just to tell you what are or how are some features called. In case you want to review the theory, you can read my previous post, where I give a definition of each of the concepts mentioned on the code. You can find it here: http://carlosqt.blogspot.com/2010/08/new-series-languages-basics-by-example.html 


Greetings Program - Verbose
' VB.NET Basics
Imports System
Namespace VBGreetProgram
    Friend Class Greet
        ' Fields or Attributes
        Private _message As String
        Private _name As String
        Private _loopMessage As Integer
        ' Properties
        Public Property Message() As String
            Get
                Return Me._message
            End Get
            Set(ByVal value As String)
                Me._message = value
            End Set
        End Property
        Public Property Name() As String
            Get
                Return Me._name
            End Get
            Set(ByVal value As String)
                Me._name = value
            End Set
        End Property
        Public Property LoopMessage() As String
            Get
                Return Me._loopMessage
            End Get
            Set(ByVal value As String)
                Me._loopMessage = value
            End Set
        End Property
        ' Constructors
        Public Sub New()
            Me._message = ""
            Me._name = ""
            Me._loopMessage = 0
        End Sub
        ' Overloaded Constructor
        Public Sub New(ByVal message As Integer, ByVal name As String, ByVal loopMessage As Integer)
            Me._message = Me.Capitalize(message)
            Me._name = Me.Capitalize(name)
            Me._loopMessage = loopMessage
        End Sub
        ' Method 1
        Private Function Capitalize(ByVal val As String) As String
            ' "if-then-else" statement
            If Len(val) >= 1 Then
                Return UCase(val(0)) & val.Substring(1, Len(val) - 1)
            Else
                Return ""
            End If
        End Function
        ' Method 2
        Public Sub Salute()
            ' "for" statement
            For i As Integer = 0 To Me._loopMessage
                Console.WriteLine("{0} {1}!", Me._message, Me._name)
            Next
        End Sub
        ' Overloaded Method 2.1
        Public Sub Salute(ByVal message As String, ByVal name As String, ByVal loopMessage As Integer)
            ' "while" statement
            Dim i As Integer = 0
            While i < loopMessage
                Console.WriteLine("{0} {1}!", Me.Capitalize(message), Me.Capitalize(name))
                i = i + 1
            End While
        End Sub
        ' Overloaded Method 2.2
        Public Sub Salute(ByVal name As String)
            ' "switch/case" statement
            Dim dtNow As DateTime = DateTime.Now()
            Select Case dtNow.Hour
                Case 6 To 11
                    Me._message = "good morning,"
                Case 12 To 17
                    Me._message = "good afternoon,"
                Case 18 To 22
                    Me._message = "good evening,"
                Case 23, 0 To 5
                    Me._message = "good night,"
                Case Else
                    Me._message = "huh?"
            End Select
            Console.WriteLine("{0} {1}!", Me.Capitalize(Me._message), Me.Capitalize(name))
        End Sub
    End Class

    ' Console Program
    Friend Module Program
        Public Sub Main()
            ' Define object of type Greet 
            Dim g As Greet
            ' Instantiate Greet. Call Constructor
            g = New Greet()
            ' Call Set Properties
            g.Message = "hello"
            g.Name = "world"
            g.LoopMessage = 5
            ' Call Method 2
            g.Salute()
            ' Call Overloaded Method 2.1 and Get Properties
            g.Salute(g.Message, "VB.net", g.LoopMessage)
            ' Call Overloaded Method 2.2
            g.Salute("carlos")

            ' Stop and exit
            Console.WriteLine("Press any key to exit...")
            Console.Read()
        End Sub
    End Module
End Namespace

Greetings Program - Minimal
' VB.NET Basics
Imports System

Friend Class Greet
    ' Fields or Attributes
    Private _message As String, _name As String
    Private _loopMessage As Integer
    ' Properties
    Property Message() As String
        Get
            Return _message
        End Get
        Set(ByVal value As String)
            _message = value
        End Set
    End Property
    Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property
    Property LoopMessage() As String
        Get
            Return _loopMessage
        End Get
        Set(ByVal value As String)
            _loopMessage = value
        End Set
    End Property
    ' Constructors
    Sub New()
        _message = ""
        _name = ""
        _loopMessage = 0
    End Sub
    ' Overloaded Constructor
    Sub New(ByVal message As Integer, ByVal name As String, ByVal loopMessage As Integer)
        _message = Capitalize(message)
        _name = Capitalize(name)
        _loopMessage = loopMessage
    End Sub
    ' Method 1
    Private Function Capitalize(ByVal val As String) As String
        ' "if-then-else" statement
        If Len(val) >= 1 Then
            Return UCase(val(0)) & val.Substring(1, Len(val) - 1)
        Else
            Return ""
        End If
    End Function
    ' Method 2
    Sub Salute()
        ' "for" statement
        For i As Integer = 0 To _loopMessage
            Console.WriteLine("{0} {1}!", _message, _name)
        Next
    End Sub
    ' Overloaded Method 2.1
    Sub Salute(ByVal message As String, ByVal name As String, ByVal loopMessage As Integer)
        ' "while" statement
        Dim i As Integer = 0
        While i < loopMessage
            Console.WriteLine("{0} {1}!", Capitalize(message), Capitalize(name))
            i = i + 1
        End While
    End Sub
    ' Overloaded Method 2.2
    Sub Salute(ByVal name As String)
        ' "switch/case" statement
        Dim dtNow As DateTime = DateTime.Now()
        Select Case dtNow.Hour
            Case 6 To 11
                _message = "good morning,"
            Case 12 To 17
                _message = "good afternoon,"
            Case 18 To 22
                _message = "good evening,"
            Case 23, 0 To 5
                _message = "good night,"
            Case Else
                _message = "huh?"
        End Select
        Console.WriteLine("{0} {1}!", Capitalize(_message), Capitalize(name))
    End Sub
End Class

' Console Program
Friend Module Program
    Sub Main()
        ' Define object of type Greet and Instantiate Greet. Call Constructor        
        Dim g As Greet = New Greet()
        ' Call Set Properties
        g.Message = "hello"
        g.Name = "world"
        g.LoopMessage = 5
        ' Call Method 2
        g.Salute()
        ' Call Overloaded Method 2.1 and Get Properties
        g.Salute(g.Message, "VB.net", g.LoopMessage)
        ' Call Overloaded Method 2.2
        g.Salute("carlos")

        ' Stop and exit
        Console.WriteLine("Press any key to exit...")
        Console.Read()
    End Sub
End Module

And the Output is:


Auto-Implemented Properties in VB.NET

Auto-implemented properties enable you to quickly specify a property of a class without having to write code to Get and Set the property. When you write code for an auto-implemented property, the Visual Basic compiler automatically creates a private field to store the property variable in addition to creating the associated Get and Set procedures. Taken from: http://msdn.microsoft.com/en-us/library/dd293589.aspx


' That means that we can omit the Fields/Attributes declaration
   'and go directly to the properties
    ' Fields or Attributes
    ' Private _message As String
    ' Private _name As String
    ' Private _loopMessage As Integer
    ' Auto-Implemented Properties
    Property Message() As String
    Property Name() As String
    Property LoopMessage() As String

' then, when ever you want to use them you get and set their value using the  properties or using the auto-generated attributes (different from C# where you need to use the property) ' Let's see an example in our constructor
        ' Constructor
        Sub New()
        ' accessing through the Property
        Message = ""
        Name = ""
        LoopMessage = 0
        ' or through the auto created fields/attributes (even if you cannot see them using "Me.")        
        _Message = ""
        _Name = ""
        _LoopMessage = 0
    End Sub  

2 comments:

  1. Hey Moi-Même!
    You should add the following to the Greetings Verbose version of the program.

    Option Explicit On
    Option Strict On

    And that will 6 more keywords to the counting to the (already) king of verbosity! (based on this blog comparisons results)

    ReplyDelete
  2. Thanks for that, I will update the code and the counters asap :)

    ReplyDelete