Variables and Data Types: Boxing and Unboxing, Enumerations, Data Type Conversion Functions, Statements

Dim declares, As types: VB.NET ના data types money માટે Decimal સાથે, boxing જ્યારે value Object માં hide થાય, Enum fixed choices માટે, અને CInt/CStr conversion family જે chop કરવાને બદલે round કરે છે.

11 min read · 10 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

1-paisa bug

ShopKeeper નો first real decision buttons સાથે કંઈ લેવાદેવા નથી ધરાવતો. customer 3 items ને Rs 10.10 each પર buy કરે છે. તમારું test total ને print કરે છે:

30.299999999999997

Mehta Uncle ને 30.30 જોઈએ છે, exactly, દરેક time; bill જે paisa દ્વારા drift થાય છે એ bill છે જે તે trust નથી કરી શકતો.

culprit એ data type choice છે: binary floating point most decimal fractions ને exactly represent નથી કરી શકતું. VB.NET પાસે money માટે built type છે, અને types ને deliberately choosing એ આ lesson છે.

Theory

Dim declares, As types

VB.NET નું declaration sentence જેવું reads થાય છે:

Dim price As Double = 350.5

Dim shopName As String = "Mehta Stores"

Dim billDate As Date = Today

notice કરો કે દરેક line પછી શું missing છે: કોઈ semicolon નહીં. VB.NET statement line ends થાય ત્યાં ends થાય છે (trailing underscore એક ને next line પર continue કરે છે). comments apostrophe ' થી start થાય છે. Java થી unlearn કરવા માટે બે habits, gain કરવા માટે બે exams' worth ના syntax marks.

At a glance

types જે ShopKeeper ને care છે

TypeSizeતેના માટે Use કરો
Integer4 bytesCounts: quantity, bill number
Long8 bytesVery large counts
Double8 bytesScientific values; money નહીં
Decimal16 bytesMoney: exact decimal arithmetic
Char / String2 bytes / variesOne symbol / text
Booleanplatform પર depend કરે છેTrue/False: paid or not
Date8 bytesBill date and time

Practical

Types, Enum, અને till પર conversions

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 થી Integer

        Dim o As Object = 42                ' boxing: value into Object
        Dim back As Integer = CInt(o)       ' unboxing
    End Sub
End Module

Theory

Boxing, unboxing, અને શા માટે તેઓ exist કરે છે

Dim o As Object = 42 innocent look કરે છે, પણ Integer એ value type છે (તેના variable માં directly lives કરે છે) જ્યારે Object variables references ને hold કરે છે. fit થવા માટે, CLR 42 ને box કરે છે: તેને heap પર new object માં copy કરે છે અને reference ને store કરે છે.

CInt(o) unbox કરે છે: value ને back out copy કરે છે.

શા માટે care? Object એ universal container છે (જૂના collections જેમ કે ArrayList Objects ને store કરે છે, next lessons), અને દરેક box/unbox એ copy ને cost કરે છે. exam definition: boxing value type ને reference type માં convert કરે છે; unboxing તેને reverse કરે છે.

Quiz

Dim r As Integer = CInt(3.7) r માં શું lands થાય છે, અને તે last semester ના Java ના (int) 3.7 થી કેવી રીતે differ કરે છે?

  1. 4: CInt nearest whole number પર round કરે છે; Java ના cast એ 3 પર chopped કર્યું
  2. 3: CInt exactly Java ના cast જેવું truncate કરે છે
  3. Compile error: Double VB.NET માં Integer નથી બની શકતું
  4. 3.7: VB.NET value ને retain કરે છે અને ફક્ત label ને change કરે છે
Show the answer

4: CInt nearest whole number પર round કરે છે; Java ના cast એ 3 પર chopped કર્યું

CInt એ CONVERSION function છે, bit-chopping cast નહીં: તે nearest whole number પર round કરે છે, એટલે 3.7 એ 4 બને છે. Java ના (int) 3.7 એ 3 પર truncate કર્યું, અને તે instinct ને VB.NET માં carry કરવું દરેક rounding question ને mis-predict કરે છે. Option C describe કરે છે કે શું Option Strict On અને plain assignment સાથે થાત, પણ CInt એ exactly explicit conversion છે જે તેને legal બનાવે છે. Option D conversion ને misunderstand કરે છે: value genuinely type ને change કરે છે. curious માટે bonus: exact .5 પર, CInt nearest EVEN number પર round કરે છે (banker's rounding), એટલે CInt(2.5) એ 2 છે.

Think first

CInt vs Val, messy-input round

till typed = "12abc" ને receive કરે છે. tap કરતા પહેલા: CInt(typed) અને Val(typed) દરેક તેના સાથે શું કરે છે?

Show the answer

CInt("12abc") exception throw કરે છે: આખી string valid number હોવી જોઈએ, અને "12abc" નથી. Val("12abc") 12 return કરે છે: Val ડાબી બાજુ થી digits ને read કરે છે અને quietly stop થાય છે first character પર જે તે use નથી કરી શકતું (Val("abc") 0 આપે છે). એટલે Val forgives કરે છે, CInt enforce કરે છે. ShopKeeper ના price field માટે તમે usually enforcement ને WANT કરો છો (typo ને caught થવું જોઈએ, half-read નહીં), એટલે validate અને convert કરો: exam phrase એ છે કે Val leading numeric characters ને parse કરે છે જ્યારે CInt fully numeric string ને require કરે છે.

Watch out

money rule અને Enum rule

ક્યારેય Double સાથે bill ન બનાવો: binary floating point 10.10 ને exactly hold નથી કરી શકતું, અને paisa errors દિવસભર ના bills પર compound થાય છે. Decimal decimal digits માં compute કરે છે: money માટે exact, અને Unit 5 ના database નો દરેક currency column તેના પર map થાય છે.

Enum values strings નથી: PayMode.UPI એ named number છે (0, 1, 2 default દ્વારા), "UPI" નહીં. mode = "UPI" ને compare કરવું fail થાય છે; PayMode.UPI સાથે compare કરો. Enums exist કરે છે invalid choices ને type કરવા impossible બનાવવા.

Theory

VB ને Java eyes સાથે reading કરવું

translation card ને week માટે keep કરો: Dim x As Integer એ int x છે; apostrophe એ // છે; કોઈ semicolons નહીં; & strings ને join કરે છે (+ પણ strings માટે work કરે છે પણ & ક્યારેય arithmetic સાથે surprise નથી કરતો). IDEAS BCA403 થી unchanged છે: types, declarations, conversions; ફક્ત costume differ થાય છે. next lesson costume richer થાય છે: VB ના string અને date functions, જ્યાં positions 1 થી start થાય છે અને exam તેને જાણે છે.

Summary

Key takeaways

  • Dim name As Type = value; statements line end પર ends થાય છે, comments apostrophe થી start થાય છે.
  • Decimal (16 bytes, exact decimal digits) એ money type છે; Double paisa દ્વારા drift થાય છે.
  • Boxing value type ને Object માં wrap કરે છે heap પર; unboxing (CInt(o)) તેને back out copy કરે છે.
  • Enum fixed menu of named choices ને define કરે છે: PayMode.Cash, .Card, .UPI.
  • Conversion family: CInt, CDbl, CStr, CBool, CDate, CType; CInt ROUNDS કરે છે (Java ના cast chopped).
  • Val leading digits ને parse કરે છે અને forgives કરે છે; CInt fully numeric string ને demand કરે છે અને otherwise throw કરે છે.
  • Memory hook: money એ Decimal છે, boxes copies ને cost કરે છે, CInt round કરે છે.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Programming in Visual Basic .NET

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati