Visual Basic

What Are The Operators In Visual Basic

Operators in Visual Basic are symbols or keywords that act on values. They let you calculate results, assign values, compare expressions, combine conditions and join strings. The main groups are arithmetic, assignment, comparison, logical and bitwise operators. This guide uses Visual Basic for .NET syntax.

Which licences fit your PC setup?

Prices are an AUD snapshot, not a live quote. Confirm variant and total on the listing.

Choose an operator for the task

The values an operator works on are called operands. In the expression 5 + 3, the operands are 5 and 3, and the result is 8. Some operators need two operands; others, such as Not, act on one.

Task Operators Example
Calculate a numeric result +, -, *, /, \, Mod, ^ 7 Mod 3 returns 1
Assign or update a value =, +=, -=, *=, /= and others count += 1 increases count by one
Compare values =, <>, <, >, <=, >= 5 > 3 returns True
Combine Boolean conditions And, Or, Xor, Not, AndAlso, OrElse True AndAlso False returns False
Join strings & "Hello" & "!" returns "Hello!"
Manipulate integer bits And, Or, Xor, Not, <<, >> 6 And 3 returns 2

Arithmetic operators: calculate numbers

Use + for addition, - for subtraction and * for multiplication. A minus sign can also negate a value: if amount is 5, then -amount is -5. Exponentiation uses ^, so 2 ^ 3 evaluates to 8.

Choose between division, integer division and remainder

Visual Basic provides three distinct operations:

  • / performs division: 7 / 2 evaluates to 3.5.
  • \ performs integer division: 7 \ 2 evaluates to 3.
  • Mod returns the remainder: 7 Mod 2 evaluates to 1.

For example, integer division can calculate how many complete groups fit into a quantity, while Mod finds what remains. Visual Basic uses Mod for this operation; % is not its remainder operator. Microsoft's arithmetic operator guide explains these operations and their data type requirements.

Check the types of both operands and the destination variable. A fractional result assigned to an Integer cannot retain its fraction. Using Option Strict On helps identify implicit narrowing conversions at compile time instead of relying on conversions that may lose information.

Assignment operators: store and update values

In the statement total = 12, the equals sign assigns 12 to total. Compound assignment combines an operation with an update. For a simple numeric variable, total += 3 has the same effect as total = total + 3.

  • count += 1 increases count by one.
  • count -= 1 decreases count by one.
  • total *= 2 doubles total.
  • total /= 2 divides total by two, subject to the variable's type.
  • message &= "!" appends an exclamation mark to message.

Visual Basic does not use ++ or -- for incrementing and decrementing. It also has no %= assignment operator: write remainder = remainder Mod divisor. Other supported forms include \=, ^=, <<= and >>=, as listed in Microsoft's assignment operator reference.

Assignment also appears in form controls. For example, TextBox1.Text = "" assigns an empty string to a text box. A clear button in Visual Basic puts this into a practical event handler.

Comparison operators: test values and references

For ordinary numeric values, comparisons return a Boolean result: True or False. Use = for equality, <> for inequality, < and > for strict comparisons, and <= or >= when equality should also count.

The meaning of = depends on context. In total = 10 it assigns a value. In If total = 10 Then it tests whether total equals 10. Visual Basic uses = for this comparison, rather than ==.

Use Is and IsNot to compare object references. The expression customer Is Nothing checks whether customer has no object reference. TypeOf customer Is Customer checks whether the referenced object is compatible with the Customer type. These checks answer different questions from comparing two numeric values.

Logical operators: combine conditions safely

With Boolean operands, And requires both conditions to be True; Or requires at least one. Xor returns True when exactly one condition is True. Not reverses a Boolean value, so Not True evaluates to False.

AndAlso and OrElse provide short-circuit evaluation. AndAlso skips the right-hand expression when the left-hand expression is False. OrElse skips it when the left-hand expression is True. And and Or evaluate both sides.

For example, customer IsNot Nothing AndAlso customer.IsActive checks the reference before reading its property. If customer is Nothing, the property access is skipped. Replacing AndAlso with And removes that protection. Microsoft's logical and bitwise operator guide describes this evaluation behaviour.

For a form example, CheckBox1.Checked AndAlso CheckBox2.Checked tests whether both boxes are selected. Learn how to read that property when coding a checkbox in Visual Basic.

String, conditional and bitwise operators

Join text with the ampersand

Use & to make string concatenation explicit. The statement fullName = firstName & " " & lastName joins two names with a space. Although + can also concatenate strings, its numeric role makes mixed-type expressions harder to interpret.

Select a value with If

The three-argument If operator selects between two values. For example, If(temperature >= 30, "Hot", "Not hot") returns "Hot" when the condition is True. Only the selected value expression is evaluated.

The two-argument form provides a fallback for a reference or nullable value. If(displayName, "Guest") returns "Guest" when displayName is Nothing; an empty string remains an empty string. Visual Basic uses these forms instead of C#'s ?: and ?? syntax. See Microsoft's If operator reference for both forms.

Work with individual bits

With integer operands, And, Or, Xor and Not operate on bit patterns. And keeps bits set in both operands; Or keeps bits set in either; Xor keeps bits that differ; Not flips the bits. The << and >> operators shift a bit pattern left or right. These operations are useful for flags and masks. AndAlso and OrElse do not perform bitwise operations.

Use parentheses to make evaluation clear

Operator precedence determines evaluation order. For example, 2 + 3 * 4 returns 14 because multiplication precedes addition. Adding parentheses changes the grouping: (2 + 3) * 4 returns 20. Microsoft's precedence reference lists the full order.

To practise, open a small project and work through these checks:

  1. Compare the results of 7 / 2, 7 \ 2 and 7 Mod 2.
  2. Declare an Integer variable, then update it using += 1.
  3. Combine two Boolean comparisons with AndAlso and change each operand to observe the result.

Run those expressions in your debugger, then choose a form exercise from the Visual Basic tutorials and apply one operator in its event handler.

Other editions for separate workloads

Other editions for separate workloads; prices in AUD, confirm the total on the listing.

How to check this worked

  1. Open Settings, System, Activation and confirm the edition shown.
  2. Enter the key and run activation; expect a green tick within a minute.
  3. Reboot and re-check activation survives the restart.

FAQ

What else should you know before buying?

What do I need before starting?
Read the requirements and confirm the edition first; the steps assume the machine is compatible.
How do I know the licence is genuine?
Activate on Microsoft's servers and check Settings, System, Activation. A working activation is the test.
Is this a subscription?
Keys sold here are one-time lifetime licences. Microsoft 365 plans are billed by Microsoft instead.
What if the key fails?
Email hi@ms.codes with the order number and a screenshot. Replacement or refund applies within 30 days.

Check who prepared and reviewed this guide

Written by
MS.Codes editorial
Reviewed by
Support team

Last verified: (source review; see test status above).

Recent Post