Theory
The 1-paisa bug
ShopKeeper's first real decision has nothing to do with buttons. A customer buys 3 items at Rs 10.10 each. Your test prints the total:
30.299999999999997
Mehta Uncle wants 30.30, exactly, every time; a bill that drifts by a paisa is a bill he cannot trust.
The culprit is a data type choice: binary floating point cannot represent most decimal fractions exactly. VB.NET has a type built for money, and choosing types deliberately is this lesson.
Theory
Dim declares, As types
VB.NET's declaration reads like a sentence:
Dim price As Double = 350.5
Dim shopName As String = "Mehta Stores"
Dim billDate As Date = Today
Notice what is missing after each line: no semicolon. A VB.NET statement ends where the line ends (a trailing underscore continues one onto the next line). Comments start with an apostrophe '. Two habits to unlearn from Java, two exams' worth of syntax marks to gain.
At a glance
The types ShopKeeper cares about
| Type | Size | Use it for |
|---|---|---|
| Integer | 4 bytes | Counts: quantity, bill number |
| Long | 8 bytes | Very large counts |
| Double | 8 bytes | Scientific values; NOT money |
| Decimal | 16 bytes | Money: exact decimal arithmetic |
| Char / String | 2 bytes / varies | One symbol / text |
| Boolean | depends on platform | True/False: paid or not |
| Date | 8 bytes | Bill date and time |
Practical
Types, an Enum, and conversions at the till
Module TillDemo
Enum PayMode ' a fixed menu of choices
Cash
Card
UPI
End Enum
Sub Main()
Dim price As Decimal = 10.1D ' D suffix: Decimal literal
Dim qty As Integer = 3
Dim total As Decimal = price * qty
MsgBox("Total: " & CStr(total)) ' 30.3 exactly
Dim mode As PayMode = PayMode.UPI
Dim typed As String = "350"
Dim amount As Integer = CInt(typed) ' String to Integer
Dim o As Object = 42 ' boxing: value into Object
Dim back As Integer = CInt(o) ' unboxing
End Sub
End Module
Theory
Boxing, unboxing, and why they exist
Dim o As Object = 42 looks innocent, but Integer is a value type (lives directly in its variable) while Object variables hold references. To fit, the CLR boxes the 42: copies it into a new object on the heap and stores the reference.
CInt(o) unboxes: copies the value back out.
Why care? Object is the universal container (old collections like ArrayList store Objects, next lessons), and each box/unbox costs a copy. Exam definition: boxing converts a value type to a reference type; unboxing reverses it.
Quiz
Dim r As Integer = CInt(3.7) What lands in r, and how does that differ from Java's (int) 3.7 from last semester?
- 4: CInt rounds to the nearest whole number; Java's cast chopped to 3
- 3: CInt truncates exactly like Java's cast
- Compile error: a Double cannot become an Integer in VB.NET
- 3.7: VB.NET keeps the value and only changes the label
Show the answer
4: CInt rounds to the nearest whole number; Java's cast chopped to 3
CInt is a CONVERSION function, not a bit-chopping cast: it rounds to the nearest whole number, so 3.7 becomes 4. Java's (int) 3.7 truncated to 3, and carrying that instinct into VB.NET mis-predicts every rounding question. Option C describes what would happen with Option Strict On and a plain assignment, but CInt is precisely the explicit conversion that makes it legal. Option D misunderstands conversion: the value genuinely changes type. Bonus for the curious: on an exact .5, CInt rounds to the nearest EVEN number (banker's rounding), so CInt(2.5) is 2.
Think first
CInt vs Val, the messy-input round
The till receives typed = "12abc". Before tapping: what do CInt(typed) and Val(typed) each do with it?
Show the answer
CInt("12abc") throws an exception: the whole string must be a valid number, and "12abc" is not. Val("12abc") returns 12: Val reads digits from the left and quietly stops at the first character it cannot use (Val("abc") gives 0). So Val forgives, CInt enforces. For ShopKeeper's price field you usually WANT enforcement (a typo should be caught, not half-read), so validate and convert: the exam phrase is that Val parses leading numeric characters while CInt requires a fully numeric string.
Watch out
The money rule and the Enum rule
Never bill with Double: binary floating point cannot hold 10.10 exactly, and paisa errors compound across a day of bills. Decimal computes in decimal digits: exact for money, and every currency column in Unit 5's database maps to it.
Enum values are not strings: PayMode.UPI is a named number (0, 1, 2 by default), not "UPI". Comparing mode = "UPI" fails; compare with PayMode.UPI. Enums exist to make invalid choices impossible to type.
Theory
Reading VB with Java eyes
Keep a translation card for the week: Dim x As Integer is int x; apostrophe is //; no semicolons; & joins strings (+ also works for strings but & never surprises you with arithmetic). The IDEAS are unchanged from BCA403: types, declarations, conversions; only the costume differs. Next lesson the costume gets richer: VB's string and date functions, where positions start at 1 and the exam knows it.
Summary
Key takeaways
- Dim name As Type = value; statements end at line end, comments start with an apostrophe.
- Decimal (16 bytes, exact decimal digits) is the money type; Double drifts by paisa.
- Boxing wraps a value type into an Object on the heap; unboxing (CInt(o)) copies it back out.
- Enum defines a fixed menu of named choices: PayMode.Cash, .Card, .UPI.
- Conversion family: CInt, CDbl, CStr, CBool, CDate, CType; CInt ROUNDS (Java's cast chopped).
- Val parses leading digits and forgives; CInt demands a fully numeric string and throws otherwise.
- Memory hook: money is Decimal, boxes cost copies, CInt rounds.