In computer programming, operators come in handy in performing various operations on data. Among them, relational operators are fundamental for making decisions, comparisons, and controlling the flow of a program. In Visual Basic .NET (VB .NET), a robust set of relational operators is available to compare values and determine the relationships between them. In this article, we’ll explore VB .NET relational operators, and provide code examples to help you understand their functionality and, more importantly, how to effectively use them in your code.
What Are Relational Operators?
Relational operators are symbols or keywords in VB .NET that enable developers to compare values and determine relationships between them. These operators return a Boolean result, indicating whether the specified relationship is true or false. VB .NET provides a set of operators that allow developers to perform these comparisons easily and effectively.
Less Than Operator (<)
The less than operator (<) is used to compare two values, and it returns True if the left operand is less than the right operand.
Module Scratchpad
Sub Main()
Dim a As Integer = 5
Dim b As Integer = 10
If a < b Then
Console.WriteLine("a is less than b")
Else
Console.WriteLine("a is not less than b")
End If
End Sub
End Module
In this example, the output will be “a is less than b” because 5 is indeed less than 10.
Less Than or Equal To Operator (<=)
Similar to the less than operator, the less than or equal to operator (<=) returns True if the left operand is less than or equal to the right operand.
Module Scratchpad
Sub Main()
Dim a As Integer = 15
Dim b As Integer = 20
If a <= b Then
Console.WriteLine("a is less than or equal to b")
Else
Console.WriteLine("a is greater than b")
End If
End Sub
End Module
Here, the output will be “a is less than or equal to b” since 15 is less than 20.
Greater Than Operator (>)
The greater than operator (>) is used to check if the left operand is greater than the right operand.
Module Scratchpad
Sub Main()
Dim a As Double = 7.5
Dim b As Double = 4.0
If a > b Then
Console.WriteLine("a is greater than b")
Else
Console.WriteLine("a is not greater than b")
End If
End Sub
End Module
The output will be “a is greater than b” because 7.5 is indeed greater than 4.0.
Greater Than or Equal To Operator (>=)
Just like the less than or equal to operator, the greater than or equal to operator (>=) returns True if the left operand is greater than or equal to the right operand.
Module Scratchpad
Sub Main()
Dim a As Double = 10.0
Dim b As Double = 10.0
If a >= b Then
Console.WriteLine("a is greater than or equal to b")
Else
Console.WriteLine("a is less than b")
End If
End Sub
End Module
In this example, the output will be “a is greater than or equal to b” since both values are equal.
Equality Operators
Equality Operator (=)
The equality operator (=) is used to check if two values are equal. It returns True if the values are equal; otherwise, it returns False.
Module Scratchpad
Sub Main()
Dim a As String = "Hello"
Dim b As String = "Hello"
If a = b Then
Console.WriteLine("a is equal to b")
Else
Console.WriteLine("a is not equal to b")
End If
End Sub
End Module
The output will be “a is equal to b” since both strings have the same content.
Inequality Operator (<>)
Conversely, the inequality operator (<>) checks if two values are not equal. If the values are different, it returns True; otherwise, it returns False.
Module Scratchpad
Sub Main()
Dim a As String = "Edward"
Dim b As String = "George"
If a <> b Then
Console.WriteLine("a is not equal to b")
Else
Console.WriteLine("a is equal to b")
End If
End Sub
End Module
The output here will be “a is not equal to b” since “Edward” is not the same as “George.”
Specialized Operators
Is Operator
The Is operator is used for reference comparison. It checks if two object references refer to the same object.
Module Scratchpad
Sub Main()
Dim arr1() As Integer = {1, 2, 3}
Dim arr2() As Integer = {1, 2, 3}
Dim arr3() As Integer = arr1
If arr1 Is arr2 Then
Console.WriteLine("arr1 and arr2 refer to the same object")
Else
Console.WriteLine("arr1 and arr2 do not refer to the same object")
End If
If arr1 Is arr3 Then
Console.WriteLine("arr1 and arr3 refer to the same object")
Else
Console.WriteLine("arr1 and arr3 do not refer to the same object")
End If
End Sub
End Module
This example produces 2 outputs; the first output will be “arr1 and arr2 do not refer to the same object” since arr1 and arr2 are distinct arrays with different memory locations. The second output will be “arr1 and arr3 refer to the same object” as arr3 is assigned the reference of arr1, making them point to the same memory location.
IsNot Operator
The IsNot operator, on the other hand, is the negation of the Is operator. It checks if two object references do not refer to the same object.
Module Scratchpad
Sub Main()
Dim obj1 As Object = New Object()
Dim obj2 As Object = New Object()
Dim obj3 As Object = obj1
If obj1 IsNot obj2 Then
Console.WriteLine("obj1 and obj2 do not refer to the same object")
Else
Console.WriteLine("obj1 and obj2 refer to the same object")
End If
If obj1 IsNot obj3 Then
Console.WriteLine("obj1 and obj3 do not refer to the same object")
Else
Console.WriteLine("obj1 and obj3 refer to the same object")
End If
End Sub
End Module
In this example, two outputs are generated. The first one, “obj1 and obj2 do not refer to the same object,” indicates that obj1 and obj2 are distinct objects. The second output, “obj1 and obj3 refer to the same object,” signifies that obj1 and obj3 reference the same object. This result is due to the assignment of obj1 to obj3.
TypeOf…Is Operator
The TypeOf…Is operator is a powerful tool in VB .NET, allowing developers to check the type of an object at runtime. This operator is particularly useful when dealing with inheritance, polymorphism, and object-oriented programming concepts. It’s only applicable to reference types.
Module Scratchpad
Public Class Animal
End Class
Public Class Dog
Inherits Animal
End Class
Sub Main()
Dim myAnimal As Animal = New Dog()
If TypeOf myAnimal Is Dog Then
Console.WriteLine("The object is of type Dog")
Else
Console.WriteLine("The object is not of type Dog")
End If
End Sub
End Module
In this example, we define two classes, Animal and Dog, where Dog inherits from Animal. We then create an instance of Dog and assign it to a variable of type Animal. Using TypeOf…Is, we can dynamically check if the object is of a specific type at runtime.
Pattern Matching
Like Operator
The Like operator in VB .NET is used for pattern-matching within strings. It allows developers to perform comparisons using wildcard characters, offering a wide range of possibilities for matching patterns. Let’s explore the wildcard characters supported by the Like operator:
- ? (Question Mark): Represents any single character.
- * (Asterisk): Denotes zero or more characters.
- # (Number Sign): Matches any single digit (0–9).
- [charlist]: Matches any single character in the specified charlist.
- [!charlist]: Matches any single character not in the specified charlist.
Let’s start with some basic examples to illustrate the usage of the Like operator.
Module Scratchpad
Sub Main()
Dim fruit As String = "apple"
' Using ? to match any single character
If fruit Like "a?ple" Then
Console.WriteLine("Pattern matched!")
Else
Console.WriteLine("Pattern not matched!")
End If
End Sub
End Module
In this example, the pattern “a?ple” will match strings like “ample,” “axple,” and “azple,” among others. The “?” wildcard represents any single character in the specified position.
Module Scratchpad
Sub Main()
Dim animals As String = "cat"
' Using * to match zero or more characters
If animals Like "c*" Then
Console.WriteLine("Pattern matched!")
Else
Console.WriteLine("Pattern not matched!")
End If
End Sub
End Module
Here, the pattern “c” will match any string starting with “c,” such as “cat,” “cow,” and “camel.” The “” wildcard denotes zero or more characters.
Advanced Usage
Now, let’s explore more advanced scenarios using the [charlist] and [!charlist] wildcards.
Module Scratchpad
Sub Main()
Dim numbers As String = "123"
' Using # to match any single digit
If numbers Like "###" Then
Console.WriteLine("Pattern matched!")
Else
Console.WriteLine("Pattern not matched!")
End If
End Sub
End Module
In this example, the pattern “###” will match any three-digit number, such as “123,” “456,” and “789.”
Module Scratchpad
Sub Main()
Dim characters As String = "hello"
' Using [charlist] to match any single character in the specified charlist
If characters Like "[aeiou]*" Then
Console.WriteLine("Pattern matched!")
Else
Console.WriteLine("Pattern not matched!")
End If
End Sub
End Module
Here, the pattern “[aeiou]*” will match strings starting with a vowel, like “apple,” “egg,” and “orange.”
Module Scratchpad
Sub Main()
Dim usernames As String = "EdwardAlgorist"
' Using [!charlist] to match any single character not in the specified charlist
If usernames Like "[!0-9]*" Then
Console.WriteLine("Pattern matched!")
Else
Console.WriteLine("Pattern not matched!")
End If
End Sub
End Module
In this example, the pattern “[!0-9]*” will match strings that do not start with a digit, ensuring that only alphanumeric usernames are considered.
Conclusion
In conclusion, understanding VB .NET relational operators is fundamental for building effective and dynamic programs. These operators enable developers to make decisions based on the relationships between variables, enabling the creation of responsive and logic-driven applications. Practice, experiment, and apply these concepts in your projects to solidify your understanding and become a more proficient VB .NET developer.