FazBrowse Wikipedia Viewer | Main Page |
Search Wikipedia:
|
Viewing: https://en.wikipedia.org/wiki/Visual_Basic_.NET

Visual Basic (.NET)

Contents
Syntax
    Simple example
    Comparison with the classic Visual Basic

[[Type system#Safely and unsafely typed systems|both safe and unsafe]],<ref name=\"option strict explicit\" /> [[Nominative type system|nominative]]"},"dialects":{"wt":"Microsoft Visual Basic"},"screenshot caption":{"wt":""},"paradigm":{"wt":"[[Programming paradigm#Multi-paradigm|Multi-paradigm]]: [[Structured programming|structured]], [[Imperative programming|imperative]], [[Object-oriented programming|object-oriented]], [[Declarative programming|declarative]], [[generic programming|generic]], [[reflective programming|reflective]] and [[Event-driven programming|event-driven]]"},"platform":{"wt":"[[.NET Framework]], [[Mono (software)|Mono]], [[.NET]]<ref>{{cite web|url=https://blogs.msdn.microsoft.com/vbteam/2018/11/12/visual-basic-in-net-core-3-0/|title=Visual Basic in .NET Core 3.0|first=Kathleen|last=Dollard|website=blogs.msdn.microsoft.com|date=November 13, 2018|access-date=November 21, 2018|archive-date=November 19, 2018|archive-url=https://web.archive.org/web/20181119070011/https://blogs.msdn.microsoft.com/vbteam/2018/11/12/visual-basic-in-net-core-3-0/|url-status=live}}</ref><ref name=\"devblogs-2020-03-11\">{{cite web |url=https://devblogs.microsoft.com/vbteam/visual-basic-support-planned-for-net-5-0/ |title=Visual Basic support planned for .NET 5.0 &#124; Visual Basic Blog |publisher=Blogs.msdn.microsoft.com |date=2020-03-11 |access-date=2020-08-26 |archive-date=January 5, 2022 |archive-url=https://web.archive.org/web/20220105022953/https://devblogs.microsoft.com/vbteam/visual-basic-support-planned-for-net-5-0/ |url-status=live }}</ref>"},"operating_system":{"wt":"Chiefly [[Windows]]<br/>Also on [[Android (operating system)|Android]], [[BSD]], [[iOS (Apple)|iOS]], [[Linux]], [[macOS]], [[Solaris (operating system)|Solaris]], and [[Unix]] <!-- Mono implementations, remember? -->"},"license":{"wt":"[[Roslyn (compiler)|Roslyn]] compiler: [[MIT License]]<ref>{{Cite web|url=https://github.com/dotnet/roslyn/blob/0d04884c243978cdd609e219fdb63cb58ebec69d/License.txt|title=Dotnet/Roslyn|website=[[GitHub]]|date=November 26, 2024}}</ref>"},"website":{"wt":"{{URL|https://docs.microsoft.com/dotnet/visual-basic/}}"},"file_ext":{"wt":"<code>.vb</code>"}},"i":0}}]}'>
Visual Basic
ParadigmMulti-paradigm: structured, imperative, object-oriented, declarative, generic, reflective and event-driven
Designed byMicrosoft
DeveloperMicrosoft
First appeared2001;25 years ago (2001)

The Windows Forms library is most commonly used to create GUI interfaces in Visual Basic. All visual elements in the Windows Forms class library derive from the Control class. This provides the minimal functionality of a user interface element such as location, size, color, font, text, as well as common events like click and drag/drop. The Control class also has docking support to let a control rearrange its position under its parent.

Forms are typically designed in the Visual Studio IDE. In Visual Studio, forms are created using drag-and-drop techniques. A tool is used to place controls (e.g., text boxes, buttons, etc.) on the form (window). Controls have attributes and event handlers associated with them. Default values are provided when the control is created, but may be changed in the Visual Studio IDE GUI or in code. Many attribute values can be modified during run time by code such as based on user actions, or changes in the environment, providing a dynamic application. For example, code can be inserted into the form resize event handler to reposition a control so that it remains centered on the form, expands to fill up the form, etc. By inserting code into the event handler for a keypress in a text box, the program can automatically translate the case of the text being entered, or even prevent certain characters from being inserted.

Syntax

edit
[</span>icon<span typeof=\"mw:Entity\" id=\"mwVg\">]</span>","txt":"[icon]"}]]}'>

Visual Basic uses statements to specify actions. The most common statement is an expression statement, consisting of an expression to be evaluated, on a single line. As part of that evaluation, functions or subroutines may be called and variables may be assigned new values. To modify the normal sequential execution of statements, Visual Basic provides several control-flow statements identified by reserved keywords. Structured programming is supported by several constructs including two conditional execution constructs (If ... Then ... Else ... End If and Select Case ... Case ... End Select ) and four iterative execution (loop) constructs (Do ... Loop, For ... To, For Each, and While ... End While) . The For ... To statement has separate initialisation and testing sections, both of which must be present. (See examples below.) The For Each statement steps through each value in a list.

In addition, in Visual Basic:

  • There is no unified way of defining blocks of statements. Instead, certain keywords, such as "If ... Then" or "Sub" are interpreted as starters of sub-blocks of code and have matching termination keywords such as "End If" or "End Sub".
  • Statements are terminated either with a colon (":") or with the end of line. Multiple-line statements in Visual Basic are enabled with " _" at the end of each such line. The need for the underscore continuation character was largely removed in version 10 and later versions.
  • The equals sign ("=") is used in both assigning values to variables and in comparison.
  • Round brackets (parentheses) are used with arrays, both to declare them and to get a value at a given index in one of them. Visual Basic uses round brackets to define the parameters of subroutines or functions.
  • A single quotation mark (') or the keyword REM, placed at the beginning of a line or after any number of space or tab characters at the beginning of a line, or after other code on a line, indicates that the (remainder of the) line is a comment.

Simple example

edit

The following is a very simple Visual Basic program, a version of the classic "Hello, World!" example created as a console application:

SubMain()

This line defines a subroutine called "Main". "Main" is the entry point, where the program begins execution.

=1 WriteLine("Allowed range is 1 and {0}",Integer.MaxValue) Loop ' Output of Floyd's Triangle DimcurrentAsInteger=1 DimrowAsInteger DimcolumnAsInteger Forrow=1Torows Forcolumn=1Torow Write("{0,-2} ",current) current+=1 Next WriteLine() Next EndSub ''' <summary> ''' Like Console.ReadLine but takes a prompt string. ''' </summary> FunctionReadLine(OptionalpromptAsString=Nothing)AsString IfpromptIsNotNothingThen Write(prompt) EndIf ReturnConsole.ReadLine() EndFunction EndModule

Comparison with the classic Visual Basic

edit
ByValsenderasObject, SubbtnClose_Click(senderAsObject,eAsEventArgs)HandlesbtnClose.Click Close() EndSub

The 'cmd' prefix is replaced by the 'btn' prefix, conforming to the new convention previously mentioned.


View original on Wikipedia | Back | FazBrowse Home