Visual Basic .NET

from Wikipedia, the free encyclopedia
Visual Basic .NET
Paradigms : multiparadigmatic: structured , imperative , declarative , object-oriented
Publishing year: 2002
Developer: Microsoft
Current  version : 2017 (15.7.1)   (May 8, 2018)
Typing : static , strong , explicit (optional: dynamic , implicit )
Important implementations : Microsoft Visual Basic .NET, SharpDevelop , MonoDevelop
Influenced by: Visual Basic Classic , Java , C #
Operating system : all for which a CLI implementation exists (e.g. Microsoft's CLR of the .NET framework or Xamarins Mono )
msdn.microsoft.com/de-de/vbasic/

Visual Basic .NET (abbreviation VB.NET ) is a programming language from Microsoft that is based on the .NET Framework . It was published in 2002 and is not a simple further development of its predecessor Visual Basic 6, but has been largely redesigned. However, since version 8, Microsoft has officially referred to the language as Visual Basic again .

Visual Basic .NET produces intermediate code for a virtual machine developed by Microsoft , the common language runtime (CLR). This machine is included in the .NET Framework .

The intermediate code is only translated into machine code at runtime. This intermediate stage of the virtual machine makes it interoperable with other .NET languages, but the .NET Framework is required for execution.

Visual Basic .NET fully supports object-oriented programming and has exception handling that is also known from other programming languages ​​such as Java or C ++ .

history

At the turn of the millennium, Visual Basic had lost its original advantage over other programming languages ​​of simply creating a graphical user interface. Many other high-level languages ​​now also offered the possibility of simply creating Windows user interfaces, for example C ++ with the Microsoft Foundation Classes (MFC). In addition, Visual Basic had meanwhile developed into a complex and rather inhomogeneous product that was becoming increasingly difficult to maintain and improve. Probably for these reasons Microsoft stopped the further development of this product (despite the commercial success and the protests of many VB programmers) and integrated the programming language Visual Basic into the in-house .NET architecture. The language was changed to a great extent in order to meet the requirements of a .NET language. This enabled full object orientation and translation into the Common Intermediate Language .

2002–2005: Visual Basic .NET

With Visual Studio .NET, a Visual Studio version appeared for the first time in 2002 that was based on the .NET Framework and thus supported Visual Basic .NET. By promoting the .NET technology, which was still quite young at that time, and the name “Visual Studio .NET”, the customers got the impression that this was a product exclusively for this platform. As a result, many developers and companies reluctantly continued to use Visual Studio with classic Visual Basic.

The first update to Visual Basic 7.1 or .NET Framework 1.1 (build 4322) as well as "Visual Studio .NET 2003" only brought minor changes, which is reflected in the "small" version jump. As of Visual Basic 7.1, for example, a counter variable can be declared in the head of a For loop. Support for mobile devices has also been added. The .NET Framework 1.1 is not used as the runtime environment on these , but the “ .NET Compact Framework 1.0”.

2005–2007: Visual Basic 8 and 9

In November 2005, the first major version jump of the .NET Framework and the .NET Compact Framework to version 2.0 took place, accompanied by the release of the new Visual Basic 8 as part of "Visual Studio 2005" (the ".NET" is out omitted from the name). With this version, some restructuring of the .NET runtime library has been made. Most noticeable is the support for generic programming , which is similar to the templates known from C ++ . Many interfaces and attributes of the runtime library are also available as generic implementations. A free Express Edition of Visual Basic .NET with a limited scope of services is also available from version 2005 .

With Visual Basic 8, the operator IsNot was introduced, which attracted attention when Microsoft patented this invention. Another innovation is the My namespace, which represents various system-dependent functions in a way that the programmer can understand, such as access to computer components, the Windows registry or information about the operating system and the current user.

In 2007, parallel to the release of Windows Vista, the new Visual Basic version 9 or "Visual Studio 2008" was released. The biggest linguistic change is the support of the new .NET component LINQ . Visual Basic 9 based on the supplied with Windows Vista and Windows XP available .NET Framework 3.0 (formerly WinFX ). The most important part of the version update is the Windows Presentation Foundation , which not only replaces Windows Forms , but also the Windows internal graphics subsystem GDI .

Since November 2007, all Visual Studio 2008 versions have been based on the .NET Framework 3.5, with which the following technologies were introduced or expanded: Language Integrated Query (LINQ), new compilers for C #, Visual Basic and C ++, ASP.NET with AJAX , ADO.NET Entity Framework, data provider support for SQL Server 2008, support for .NET Framework Client Profile, and more. a.

properties

Windows applications

Windows Forms is used for developing Windows applications . This is an event- oriented programming model in which appropriate event procedures are called when the operating system takes a certain user action, such as B. pressing a button reports.

In contrast to Visual Basic 6, Windows Forms are not saved in a special format, but are created and used like normal classes that are derived from a corresponding base class (usually System.Windows.Forms.Form ).

When such a class is instantiated, the objects that are to be displayed within the window are created in the constructor . This model is also used in the Java programming language.

Modern development environments allow forms to be created visually, with the code required for the constructor being generated by the development environment.

Web applications

ASP.NET is used for the development of web applications . There is a separation between the presentation of the website and the code to be executed, which is located in a VB.NET class. This has the advantage that the spaghetti code known from ASP , which makes the maintenance of larger projects more difficult, is avoided.

The IIS from Microsoft or the Apache HTTP server with the mod_mono module is required to run ASP.NET applications .

ASP.NET also enables the development of web services . The information required for the publication of web services is generated by the development environment on the basis of attributes that the programmer inserts into the program.

For access to a web service, the development environment can generate a class on request that encapsulates access to the methods of the web service.

Special features compared to other .NET languages

As a .NET language, Visual Basic .NET is subject to the advantages and disadvantages of this platform. Visual Basic Classic did many minor things differently from other languages. For the sake of backward compatibility, many of these points are included as “legacy issues” in Visual Basic .NET. Some examples:

  • Usually, the logical value false or false converted into a number corresponds to the value 0, while true or true is interpreted as 1. However, Visual Basic Classic uses the value −1 for True (all bits to 1). This convention made it possible not to have to distinguish between a logical and a binary emergency operator. Therefore, in Visual Basic .NET, the conversions CInt(True)or CType(True, Int32)still result in the value −1, even if VB.NET now also works internally with 0 and 1. The call of the .NET Framework method Convert.ToInt32(True), however, evaluates to 1. Incidentally, the .NET Runtime regards 0 as False , everything else as True .
  • In most programming languages, the size in an array declaration is the number of entries, e.g. B. int a[5];declares an array with 5 elements (in .NET standard index 0 to 4). In VB.NET declares Dim a(5) As Integeran array with the highest index 5, thus an array with 6 elements (index 0 to 5), in order Dim a([lowerBound To] upperBound)to remain downward compatible with the VB Classic syntax .
  • Language support for XML, e.g. B. for literals or for querying and creating new elements
  • VB.NET is tolerant when initializing fields; you can use references to your own instance or other fields
  • VB.NET supports inner interfaces and inner classes in interfaces. This procedure allows you to create further versions of an interface. When the original version is implemented, the user of the interface receives the visual indication that there are other versions available and can select the appropriate one.
  • VB.NET does not need to differentiate between out and byRef parameters
  • VB.NET allows the iteration variable to be overwritten
  • VB.NET allows the use of byRef in ExtensionMethods, which allows the original instance to be initialized or replaced.
  • VB.NET has more Linq keywords than other languages. E.g. the keyword Distinct can be used instead of calling the extension method of the same name, and if the output is of the same type as the input, the Select can be omitted. This usually makes Linq expressions shorter and therefore easier to read.

Differences from Visual Basic Classic

Except for the language syntax, Visual Basic .NET is not comparable to the previous Visual Basic versions. As a full .NET language, VB.NET is completely object-oriented and the source code is translated into the Common Intermediate Language , not into native Win32 code. This code is converted into machine code at runtime and can thus be adapted to the current platform.

In addition to these changes in the background, the changeover to .NET technology also brought changes for the programmer. The .NET framework provides numerous function libraries, e.g. B. for file access or for reading and writing XML files. At the time of the introduction of VB.NET, the most important were Windows Forms , which replaced the outdated Thunder Forms system used in previous versions for creating user interfaces. The most obvious difference between the Windows Forms and the Thunder Forms is the fact that a special file format is no longer used for storage, but the forms are created as classes by code. Here is the constructor of the window class used to create the represented also by classes controls. Modern integrated development environments enable the visual creation of forms, whereby the code necessary for the constructor of the window class is generated by the development environment. Furthermore, in contrast to Visual Basic 6, Web applications can also be created with Visual Basic .NET. With the introduction of the .NET framework, Web Forms , a GUI library for the ASP.NET environment, was made available.

In addition to these advantages, the changeover also brings with it some problems. Since VB.NET does not use the Visual Basic 6 function library, but the .NET framework, a Visual Basic Classic code project can only be migrated to the new version with great effort. Microsoft provided a so-called migration assistant with its development environment Visual Studio up to version 2008. This usually converts the code structure correctly and, apart from a few special cases, masters the conversion from Thunder Forms to Windows Forms , but has severe problems with graphics programming, for example, because the graphics systems used are from Visual Basic Classic ( GDI , encapsulated in Thunder Forms control elements) and Visual Basic .NET ( GDI + ) are practically incompatible with each other. For better portability, there is next to Microsoft Migration Assistant is a function collection in the .NET Framework (in the Microsoft.VisualBasic - namespace ) that some important Visual Basic Classic features provides the .NET available. Existing COM components can continue to be used without restrictions. In addition to the problem of incompatibility, the loss of the " Edit and Continue " function in Visual Studio .NET meant a major developmental step backwards. In contrast to Visual Basic 6, it was no longer possible to change the code in debug mode and to allow execution to continue without interruption . However, this functionality was reintroduced in Visual Basic 2005.

In Visual Basic .NET, unlike in Visual Basic Classic, you can create programs even if you have not acquired a development environment. The namespace System.Reflection.Emitprovides functions for compiling from your own application; there is also a command line compiler called vbc.exe .

example

Hello world example

The following source code represents a simple Visual Basic program that outputs the message “Hello world!” On the standard output, which is usually a command window (console).

Public Class Program
   Public Shared Sub Main()
     Console.WriteLine("Hallo Welt!")
   End Sub
End Class

Explanations:

This program is a simple console application made up of a single class. In this case, the class contains only static Main()- method - in the VB nomenclature one shared sub- which is called once usually at the start of any application first.

The output instruction Console.WriteLinecomes from the .NET class library and enables the output on the standard output, whereby the output is terminated with a line break .

Simple message box

Imports System.Windows.Forms

Private Class Program
  Public Shared Sub Main()
    MessageBox.Show("Text", "Titel", MessageBoxButtons.OK, MessageBoxIcon.Hand)
  End Sub
End Class

FileStream example

Imports System.IO
Imports System.Text

Private Class Program
  Public Shared Sub Main()
    ' Text, der in die Datei geschrieben wird
    Const textToWrite As String = "Hallo Welt"
    Const fileName As String = "dateiname.txt"

    ' Datei "dateiname.txt" wird erstellt oder überschrieben
    Using stream As New FileStream(fileName, FileMode.Create, FileAccess.Write)
      ' Der Text wird UTF8-kodiert in die Datei geschrieben
      Dim data As Byte() = Encoding.UTF8.GetBytes(textToWrite)
      stream.Write(data, 0, data.Length)
    End Using 'Datei wird geschlossen.
  End Sub
End Class

Networking example

Based on Visual Basic 14 with string interpolation (Visual Studio 2015):

Imports System.Console
Imports System.Text
Imports System.Net
Imports System.Net.Sockets

''' <summary>
''' Beispielprogramm: Socketverbindung
''' </summary>
''' <remarks>Für weitere Details siehe <see cref="System.Net.Sockets.Socket"/>.</remarks>
Class Program
  ''' <summary>
  ''' Einstiegspunkt
  ''' </summary>
  Public Shared Sub Main()
    ' Daten, die gesendet werden
    Const textToSend As String = "Hallo Welt"
    ' Endpunkt, zu dem verbunden wird
    Const localhost As String = "127.0.0.1"
    Const port As Integer = 80

    Dim data As Byte() = Encoding.UTF8.GetBytes(textToSend)
    Dim ip As IPAddress = IPAddress.Parse(localhost)
    Dim ipEndPoint As New IPEndPoint(ip, port)

    ' Socket, das verwendet wird
    Using socket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
      socket.Connect(ipEndPoint)
      ' Es wird zum Endpunkt verbunden
      Dim byteCount As Integer = socket.Send(data, SocketFlags.None)
      ' Daten werden gesendet
      WriteLine($"Es wurden {byteCount} Bytes gesendet")
      Dim buffer As Byte() = New Byte(255) {}
      ' Puffer für die zu empfangenen Daten
      byteCount = socket.Receive(buffer, SocketFlags.None)
      ' Daten werden empfangen
      ' Wenn eine Antwort erhalten wurde, diese ausgeben
      If byteCount > 0 Then
        WriteLine($"Es wurden {byteCount} Bytes empfangen")
        Dim answer As String = Encoding.UTF8.GetString(buffer)
        WriteLine($"Empfangene Daten: {answer}")
      End If
    End Using  ' Verbindung wird geschlossen.
  End Sub
End Class

Development environments

The most commonly used IDE is Visual Studio , which fully supports Visual Basic. SharpDevelop up to version 4 (short: #develop ) and, with clear cutbacks , MonoDevelop , the development environment of the Mono project , can also be used.

With the command line tool vbc.exe included in .NET , Visual Basic files can be compiled , with msbuild.exe Visual Basic projects.

Legal Aspects

Unlike many modern high-level languages, Visual Basic Classic is not an open standard, but is proprietary and intellectual property of Microsoft . With Visual Basic .NET this link has been loosened, as some parts of the .NET infrastructure have been standardized by Ecma International . The patenting of the IsNot operator introduced in Visual Basic .NET 8 was, however, another step towards a closed environment.

Microsoft has since failed to patent. Microsoft now also has an open source strategy - starting with Roslyn and the .NET Foundation founded in 2014 . The Roslyn compiler environment (including compiler, syntax tree analyzer, IL emitter and much more) for VB.NET is also completely written in VB.NET and is open source (Apache 2.0 licensed).

See also

literature

Web links

Wikibooks: Visual Basic .NET  - learning and teaching materials

Individual evidence

  1. Product page Visual Basic Express Edition
  2. Patent IsNot to the operator (English)
  3. Overview of the changes in Visual Basic 9 (English)
  4. Future Visual Studio versions (English)
  5. Architecture of .NET Framework 3.5 (German)
  6. Entry on Google Patents