VB.NET Programming — Study Notes

Unit 02

Boxing & Unboxing, Date/Time Functions, String Functions & MsgBox

4 Questions 6 Marks Each VB.NET
01
6 Marks Question

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

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
Syntax
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
OutputBoxed value = 25
Unboxing

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
Syntax
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
OutputUnboxed value = 50

Key Differences Between Boxing and Unboxing

FeatureBoxingUnboxing
ConversionValue type → ObjectObject → Value type
MemoryAllocates heap memoryCopies back to stack
Conversion typeImplicitExplicit
PerformanceSlowerFaster than boxing
RiskSafeMay cause runtime error
Important Points
  • 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.
Conclusion Boxing and unboxing are essential features of VB.NET that allow smooth conversion between value types and reference types. While boxing is automatic, unboxing requires explicit conversion and must be used carefully to avoid runtime errors.
02
6 Marks Question

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.

Current Date & Time
01Now()Returns current date and time
MsgBox(Now())
Output01/01/2026 02:15:02 PM
02Today()Returns only the current date
MsgBox(Today())
Output01/01/2026
03DateString()Returns current date as a string
MsgBox(DateString())
Output01/01/2026
04TimeOfDay()Returns current system time
MsgBox(TimeOfDay())
Output02:15:02 PM
05TimeString()Returns current time as a string
MsgBox(TimeString())
Output02:15:02 PM
Extract Date Parts
06Day(Date)Returns day of given date
MsgBox(Day(#01/22/2026#))
Output22
07Month(Date)Returns month number (1–12)
MsgBox(Month(Now()))
Output01
08Year(Date)Returns year
MsgBox(Year(Now()))
Output2026
09Hour() / Minute() / Second()Extract time parts
MsgBox(Hour(Now()))
MsgBox(Minute(Now()))
MsgBox(Second(Now()))
OutputOutputs hour, minute, and second respectively.
Week & Month Name Functions
10WeekDay(Date)Returns number (1–7)
MsgBox(WeekDay(Now()))
Output3 (e.g. Tuesday)
11WeekDayName()Returns name of weekday
MsgBox(WeekDayName(WeekDay(Now())))
OutputTuesday
12MonthName()Returns month name
MsgBox(MonthName(7, False))
OutputJuly
Date Manipulation Functions
13DateAdd()Adds interval to date
MsgBox(DateAdd(DateInterval.Month, 1, #8/28/2026#))
Output09/28/2026
14DateDiff()Returns difference between two dates
MsgBox(DateDiff(DateInterval.Day, #05/01/2026#, #05/10/2026#))
Output9
15DatePart()Returns specific part of date
MsgBox(DatePart(DateInterval.Day, #05/01/2026#))
Output1
16IsDate()Checks whether value is a valid date
MsgBox(IsDate(#01/22/2026#))
OutputTrue
Conclusion VB.NET provides a rich set of Date and Time functions to obtain current date/time, extract individual parts, perform calculations, and validate date values. These functions make date and time handling easy and efficient in VB.NET applications.
03
6 Marks Question

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.

01Asc()Returns ASCII value of a character
Syntax
Asc(character)
Example
Dim n As Integer
n = Asc("A")
MsgBox(n)
Output65
02Chr()Returns character for given ASCII code
Syntax
Chr(code)
Example
Dim c As Char
c = Chr(65)
MsgBox(c)
OutputA
03InStr()Position of first occurrence of substring
Syntax
InStr([start], string1, string2, [compare])
Example
Dim pos As Integer
pos = InStr("How Do You Do?", "Do")
MsgBox(pos)
Output5
04InStrRev()Searches substring from right side
Syntax
InStrRev(string1, string2)
Example
Dim pos As Integer
pos = InStrRev("Hello World Hello", "Hello")
MsgBox(pos)
Output13
05StrReverse()Reverses a string
Syntax
StrReverse(string)
Example
Dim s As String
s = StrReverse("Hello")
MsgBox(s)
OutputolleH
06StrComp()Compares two strings, returns -1, 0, or 1
Syntax
StrComp(string1, string2, comparemethod)
Example
Dim result As Integer
result = StrComp("ABCD", "abcd", CompareMethod.Text)
MsgBox(result)
Output0
07StrConv()Converts string case
Syntax
StrConv(string, conversion)
Example
Dim s As String
s = StrConv("hello world", VbStrConv.ProperCase)
MsgBox(s)
OutputHello World
08StrDup()Repeats a character specified number of times
Syntax
StrDup(number, character)
Example
Dim s As String
s = StrDup(5, "A")
MsgBox(s)
OutputAAAAA
09Filter()Returns array elements matching a string
Syntax
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))
OutputIs
Conclusion VB.NET string functions provide powerful tools for text processing such as searching, comparing, reversing, and converting strings. These built-in functions simplify string manipulation and improve program efficiency.
04
6 Marks Question

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
OutputA message box showing: Welcome to VB.NET

MsgBox with Title

MsgBox("Welcome", , "My Application")
OutputMessage box with title: My Application

MsgBox with Buttons (Yes/No)

Dim result As Integer
result = MsgBox("Do you want to exit?", MsgBoxStyle.YesNo, "Confirm")
OutputIf user clicks Yes → returns 6   |   If user clicks No → returns 7

Common MsgBoxStyle Constants

ConstantMeaning
MsgBoxStyle.OkOnlyOK button
MsgBoxStyle.YesNoYes and No buttons
MsgBoxStyle.YesNoCancelYes, No, Cancel
MsgBoxStyle.CriticalError icon
MsgBoxStyle.InformationInfo icon
MsgBoxStyle.ExclamationWarning icon

Example with Icon

MsgBox("File not found", MsgBoxStyle.Critical, "Error")
OutputDisplays error icon with message: File not found

Return Values of MsgBox

Button ClickedReturn Value
OK1
Cancel2
Abort3
Retry4
Ignore5
Yes6
No7
Uses of MsgBox
  • Display messages to users
  • Show warnings or errors
  • Ask confirmation from user
  • Debugging during development
Conclusion MsgBox is an important VB.NET function used to interact with users through dialog boxes. It allows developers to display messages, icons, and buttons, and to capture user responses effectively.