Visual Basic .NET - Examples

Examples

The following is a very simple VB.NET program, a version of the classic "Hello world" example created as a console application:

Module Module1 Sub Main Console.WriteLine("Hello, world!") End Sub End Module

The effect is to write the text Hello, world! to the command line. Each line serves a specific purpose, as follows:

Module Module1

This is a module definition, a division of code similar to a class, although modules can contain classes. Modules serve as containers of code that can be referenced from other parts of a program.
It is common practice for a module and the code file, which contains it, to have the same name; however, this is not required, as a single code file may contain more than one module and/or class definition.

Sub Main

This is the entry point where the program begins execution. Sub is an abbreviation of "subroutine."

Console.WriteLine("Hello, world!")

This line performs the actual task of writing the output. Console is a system object, representing a command-line interface and granting programmatic access to the operating system's standard streams. The program calls the Console method WriteLine, which causes the string passed to it to be displayed on the console. Another common method is using MsgBox (a Message Box).

This piece of code is a solution to Floyd's Triangle:

Imports System.Console Module Program Sub Main Dim rows As Integer ' Input validation. Do Until Integer.TryParse(ReadLine("Enter a value for how many rows to be displayed: "), rows) AndAlso rows >= 1 WriteLine("Allowed range is 1 and {0}", Integer.MaxValue) Loop ' Output of Floyd's Triangle Dim current = 1 For row = 1 To rows For column = 1 To row Write("{0,-2} ", current) current += 1 Next WriteLine Next End Sub ''' ''' Shadows Console.ReadLine with a version which takes a prompt string. ''' Function ReadLine(Optional prompt As String = Nothing) As String If prompt IsNot Nothing Then Write(prompt) End If Return Console.ReadLine End Function End Module

Read more about this topic:  Visual Basic .NET

Famous quotes containing the word examples:

    It is hardly to be believed how spiritual reflections when mixed with a little physics can hold people’s attention and give them a livelier idea of God than do the often ill-applied examples of his wrath.
    —G.C. (Georg Christoph)

    In the examples that I here bring in of what I have [read], heard, done or said, I have refrained from daring to alter even the smallest and most indifferent circumstances. My conscience falsifies not an iota; for my knowledge I cannot answer.
    Michel de Montaigne (1533–1592)

    There are many examples of women that have excelled in learning, and even in war, but this is no reason we should bring ‘em all up to Latin and Greek or else military discipline, instead of needle-work and housewifry.
    Bernard Mandeville (1670–1733)