Makindo Medical Notes.com |
|
---|---|
Download all this content in the Apps now Android App and Apple iPhone/Pad App | |
MEDICAL DISCLAIMER:The contents are under continuing development and improvements and despite all efforts may contain errors of omission or fact. This is not to be used for the assessment, diagnosis or management of patients. It should not be regarded as medical advice by healthcare workers or laypeople. It is for educational purposes only. Please adhere to your local protocols. Use the BNF for drug information. If you are unwell please seek urgent healthcare advice. If you do not accept this then please do not use the website. Makindo Ltd |
Visual Basic (VB) is an event-driven programming language developed by Microsoft. It is designed to be easy to learn and use, providing a graphical user interface (GUI) to modify code by dragging and dropping objects and defining their behavior and appearance. VB is commonly used for developing Windows applications and automating tasks in Microsoft Office.
' This is a single-line comment
' This is a ' multi-line comment ' in Visual Basic
Dim age As Integer Dim height As Double Dim name As String Dim isActive As Boolean
If age > 18 Then Console.WriteLine("Adult") Else Console.WriteLine("Minor") End If
For i As Integer = 1 To 5 Console.WriteLine(i) Next Dim j As Integer = 1 While j <= 5 Console.WriteLine(j) j += 1 End While Dim k As Integer = 1 Do While k <= 5 Console.WriteLine(k) k += 1 Loop
Function Add(a As Integer, b As Integer) As Integer Return a + b End Function Sub Main() Dim sum As Integer = Add(5, 3) Console.WriteLine("Sum: " & sum) End Sub
Sub Greet(name As String) Console.WriteLine("Hello, " & name & "!") End Sub Sub Main() Greet("Alice") End Sub
Try Dim result As Integer = 10 / 0 Catch ex As DivideByZeroException Console.WriteLine("Cannot divide by zero!") End Try
Class Dog Public Name As String Public Age As Integer Public Sub New(name As String, age As Integer) Me.Name = name Me.Age = age End Sub Public Sub Bark() Console.WriteLine(Name & " says woof!") End Sub End Class Sub Main() Dim myDog As New Dog("Buddy", 3) myDog.Bark() End Sub
Class Animal Public Name As String Public Sub Speak() Console.WriteLine("Animal speaks") End Sub End Class Class Dog Inherits Animal Public Sub Speak() Console.WriteLine(Name & " says woof!") End Sub End Class Sub Main() Dim myDog As New Dog() myDog.Name = "Buddy" myDog.Speak() End Sub
Class Animal Public Overridable Sub Speak() Console.WriteLine("Animal speaks") End Sub End Class Class Dog Inherits Animal Public Overrides Sub Speak() Console.WriteLine("Dog barks") End Sub End Class Class Cat Inherits Animal Public Overrides Sub Speak() Console.WriteLine("Cat meows") End Sub End Class Sub Main() Dim myDog As Animal = New Dog() Dim myCat As Animal = New Cat() myDog.Speak() ' Output: Dog barks myCat.Speak() ' Output: Cat meows End Sub
' This code is auto-generated when you design a form using the Visual Studio GUI designer Public Class MainForm Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click MessageBox.Show("Button clicked!") End Sub End Class
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click MessageBox.Show("Button clicked!") End Sub
Imports System.Data.SqlClient Sub Main() Dim connectionString As String = "Data Source=(local);Initial Catalog=MyDatabase;Integrated Security=True" Dim query As String = "SELECT * FROM MyTable" Using connection As New SqlConnection(connectionString) connection.Open() Dim command As New SqlCommand(query, connection) Dim reader As SqlDataReader = command.ExecuteReader() While reader.Read() Console.WriteLine(reader("ColumnName").ToString()) End While End Using End Sub
Visual Basic is a versatile and user-friendly programming language ideal for developing Windows applications and automating tasks. It supports procedural, object-oriented, and event-driven programming. Key features include easy GUI development, robust data access, and comprehensive error handling. Understanding its syntax, control structures, object-oriented principles, and data access techniques is essential for effective Visual Basic programming.