Boxing and Unboxing in VB.NET
In VB.NET, boxing and unboxing are mechanisms used to convert between value types and reference types. These concepts are important in the .NET type system when values need to be treated as objects.
Boxing is the process of converting a value type (like Integer, Double, Boolean) into a reference type (Object).
During boxing:
- The value is copied into an object
- Memory is allocated on the heap
- The value type becomes a reference type
Dim obj As Object
Dim num As Integer = 10
obj = num ' Boxing
Example
Module Module1
Sub Main()
Dim num As Integer = 25
Dim obj As Object
obj = num ' Boxing
MsgBox("Boxed value = " & obj)
End Sub
End Module
Unboxing is the process of converting a reference type (Object) back into its original value type.
During unboxing:
- The object is converted back to value type
- Explicit conversion is required
- Value is copied from heap to stack
Dim num As Integer
num = CType(obj, Integer) ' Unboxing
Example
Module Module1
Sub Main()
Dim obj As Object = 50
Dim num As Integer
num = CType(obj, Integer)
MsgBox("Unboxed value = " & num)
End Sub
End Module
Key Differences Between Boxing and Unboxing
| Feature | Boxing | Unboxing |
|---|---|---|
| Conversion | Value type → Object | Object → Value type |
| Memory | Allocates heap memory | Copies back to stack |
| Conversion type | Implicit | Explicit |
| Performance | Slower | Faster than boxing |
| Risk | Safe | May cause runtime error |
- Boxing is automatic in VB.NET.
- Unboxing requires explicit casting using CType.
- Excessive boxing/unboxing reduces performance.
- Used when value types must be treated as objects.
Date and Time Functions in VB.NET
VB.NET provides many built-in Date and Time functions to retrieve, manipulate, and validate date/time values. These functions help programmers perform operations like getting current date, extracting parts of date, and calculating differences.
MsgBox(Now())
MsgBox(Today())
MsgBox(DateString())
MsgBox(TimeOfDay())
MsgBox(TimeString())
MsgBox(Day(#01/22/2026#))
MsgBox(Month(Now()))
MsgBox(Year(Now()))
MsgBox(Hour(Now()))
MsgBox(Minute(Now()))
MsgBox(Second(Now()))
MsgBox(WeekDay(Now()))
MsgBox(WeekDayName(WeekDay(Now())))
MsgBox(MonthName(7, False))
MsgBox(DateAdd(DateInterval.Month, 1, #8/28/2026#))
MsgBox(DateDiff(DateInterval.Day, #05/01/2026#, #05/10/2026#))
MsgBox(DatePart(DateInterval.Day, #05/01/2026#))
MsgBox(IsDate(#01/22/2026#))
String Functions in VB.NET
VB.NET provides many built-in string functions to perform operations such as searching, comparing, converting, and manipulating strings. These functions help developers process text easily in applications.
Asc(character)
Example
Dim n As Integer
n = Asc("A")
MsgBox(n)
Chr(code)
Example
Dim c As Char
c = Chr(65)
MsgBox(c)
InStr([start], string1, string2, [compare])
Example
Dim pos As Integer
pos = InStr("How Do You Do?", "Do")
MsgBox(pos)
InStrRev(string1, string2)
Example
Dim pos As Integer
pos = InStrRev("Hello World Hello", "Hello")
MsgBox(pos)
StrReverse(string)
Example
Dim s As String
s = StrReverse("Hello")
MsgBox(s)
StrComp(string1, string2, comparemethod)
Example
Dim result As Integer
result = StrComp("ABCD", "abcd", CompareMethod.Text)
MsgBox(result)
StrConv(string, conversion)
Example
Dim s As String
s = StrConv("hello world", VbStrConv.ProperCase)
MsgBox(s)
StrDup(number, character)
Example
Dim s As String
s = StrDup(5, "A")
MsgBox(s)
Filter(array, match, include, compare)
Example
Dim arr() As String = {"This","Is","It"}
Dim res() As String
res = Filter(arr, "Is", True, CompareMethod.Text)
MsgBox(res(0))
MsgBox in VB.NET
MsgBox is a built-in function in VB.NET used to display a message dialog box to the user. It is commonly used to show information, warnings, errors, or to get user responses like Yes/No.
Definition
MsgBox displays a pop-up message box containing text, buttons, and icons, and optionally returns a value indicating which button the user clicked.
Syntax
MsgBox(prompt[, buttons][, title])
Parameters
- promptMessage text to display.
- buttonsType of buttons and icon (optional).
- titleTitle of the message box (optional).
Simple Example
Module Module1
Sub Main()
MsgBox("Welcome to VB.NET")
End Sub
End Module
MsgBox with Title
MsgBox("Welcome", , "My Application")
MsgBox with Buttons (Yes/No)
Dim result As Integer
result = MsgBox("Do you want to exit?", MsgBoxStyle.YesNo, "Confirm")
Common MsgBoxStyle Constants
| Constant | Meaning |
|---|---|
| MsgBoxStyle.OkOnly | OK button |
| MsgBoxStyle.YesNo | Yes and No buttons |
| MsgBoxStyle.YesNoCancel | Yes, No, Cancel |
| MsgBoxStyle.Critical | Error icon |
| MsgBoxStyle.Information | Info icon |
| MsgBoxStyle.Exclamation | Warning icon |
Example with Icon
MsgBox("File not found", MsgBoxStyle.Critical, "Error")
Return Values of MsgBox
| Button Clicked | Return Value |
|---|---|
| OK | 1 |
| Cancel | 2 |
| Abort | 3 |
| Retry | 4 |
| Ignore | 5 |
| Yes | 6 |
| No | 7 |
- Display messages to users
- Show warnings or errors
- Ask confirmation from user
- Debugging during development