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

Dim declare करता है, As type देता है: money के लिए Decimal वाले VB.NET के data types, boxing जब कोई value एक Object में छिप जाती है, fixed choices के लिए Enum, और CInt/CStr conversion family जो chop नहीं, round करती है।

11 min read · 10 cards · 2 checks

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


Theory

1-Paisa वाला Bug

ShopKeeper का पहला असली decision buttons से कोई लेना-देना नहीं रखता। एक customer 3 items ₹10.10 हर एक के price पर खरीदता है। आपका test total print करता है:

30.299999999999997

Mehta Uncle चाहते हैं 30.30, exactly, हर बार; एक paisa से drift करने वाला bill एक ऐसा bill है जिस पर वह trust नहीं कर सकते।

Culprit एक data type choice है: binary floating point ज़्यादातर decimal fractions को exactly represent नहीं कर सकता। VB.NET के पास money के लिए बनाया गया एक type है, और deliberately types चुनना यही lesson है।

Theory

Dim Declare करता है, As Type देता है

VB.NET की declaration एक sentence जैसी पढ़ती है:

Dim price As Double = 350.5

Dim shopName As String = "Mehta Stores"

Dim billDate As Date = Today

नोटिस कीजिए हर line के बाद क्या missing है: कोई semicolon नहीं। एक VB.NET statement वहीं खत्म होता है जहाँ line खत्म होती है (एक trailing underscore इसे अगली line पर continue करता है)। Comments एक apostrophe ' से शुरू होते हैं। Java से unlearn करने के लिए दो habits, gain करने के लिए दो exams लायक syntax marks।

At a glance

वे Types जिनकी ShopKeeper को Fikar है

TypeSizeकिसके लिए इस्तेमाल करें
Integer4 bytesCounts: quantity, bill number
Long8 bytesबहुत बड़े counts
Double8 bytesScientific values; money NAHI
Decimal16 bytesMoney: exact decimal arithmetic
Char / String2 bytes / variesएक symbol / text
Booleanplatform पर depend करता हैTrue/False: paid है या नहीं
Date8 bytesBill की date और time

Practical

Till पर Types, एक Enum, और 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 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, और ये क्यों Exist करते हैं

Dim o As Object = 42 innocent लगता है, पर Integer एक value type है (सीधे अपने variable में रहता है) जबकि Object variables references hold करते हैं। Fit होने के लिए, CLR 42 को box करता है: इसे heap पर एक नए object में copy करता है और reference store करता है।

CInt(o) unbox करता है: value को वापस copy करता है।

Fikar क्यों करें? Object universal container है (पुराने collections जैसे ArrayList Objects store करते हैं, अगले lessons में), और हर box/unbox एक copy की cost रखता है। Exam definition: boxing एक value type को reference type में convert करता है; unboxing इसे reverse करता है।

Quiz

Dim r As Integer = CInt(3.7) r में क्या land होता है, और यह पिछले semester के Java के (int) 3.7 से कैसे अलग है?

  1. 4: CInt nearest whole number तक round करता है; Java का cast 3 तक chop करता था
  2. 3: CInt exactly Java के cast की तरह truncate करता है
  3. Compile error: VB.NET में एक Double, Integer नहीं बन सकता
  4. 3.7: VB.NET value रखता है और सिर्फ़ label बदलता है
Show the answer

4: CInt nearest whole number तक round करता है; Java का cast 3 तक chop करता था

CInt एक CONVERSION function है, bit-chopping cast नहीं: यह nearest whole number तक round करता है, तो 3.7, 4 बन जाता है। Java का (int) 3.7 truncate होकर 3 बना था, और वह instinct VB.NET में ले आना हर rounding question mis-predict करता है। Option C describe करता है कि Option Strict On और एक plain assignment के साथ क्या होता, पर CInt exactly वह explicit conversion है जो इसे legal बनाता है। Option D conversion को misunderstand करता है: value genuinely type बदलती है। Curious लोगों के लिए bonus: एक exact .5 पर, CInt nearest EVEN number तक round करता है (banker's rounding), तो CInt(2.5), 2 है।

Think first

CInt बनाम Val, Messy-Input Round

Till को typed = "12abc" मिलता है। Tap करने से पहले: CInt(typed) और Val(typed) हर एक इसके साथ क्या करते हैं?

Show the answer

CInt("12abc") एक exception throw करता है: पूरी string एक valid number होनी चाहिए, और "12abc" नहीं है। Val("12abc") 12 return करता है: Val बाईं तरफ़ से digits पढ़ता है और चुपचाप पहले unusable character पर रुक जाता है (Val("abc") 0 देता है)। तो Val माफ़ करता है, CInt enforce करता है। ShopKeeper के price field के लिए आप usually enforcement CHAHTE हैं (एक typo catch होनी चाहिए, आधी पढ़ी नहीं जानी चाहिए), तो validate करके convert कीजिए: exam phrase यह है कि Val leading numeric characters parse करता है जबकि CInt को एक fully numeric string चाहिए।

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 है (default से 0, 1, 2), "UPI" नहीं। mode = "UPI" compare करना fail होता है; PayMode.UPI से compare कीजिए। Enums इसलिए exist करते हैं ताकि invalid choices type करना impossible हो जाए।

Theory

Java की Eyes से VB पढ़ना

इस week के लिए एक translation card रखिए: Dim x As Integer, int x है; apostrophe, // है; कोई semicolons नहीं; & strings जोड़ता है (+ भी strings के लिए काम करता है पर & कभी arithmetic से surprise नहीं करता)। IDEAS BCA403 से unchanged हैं: types, declarations, conversions; सिर्फ़ costume अलग है। अगला lesson costume को richer बनाता है: VB के string और date functions, जहाँ positions 1 से शुरू होते हैं और exam को यह पता है।

Summary

Key takeaways

  • Dim name As Type = value; statements line end पर खत्म होते हैं, comments एक apostrophe से शुरू होते हैं।
  • Decimal (16 bytes, exact decimal digits) money का type है; Double paisa से drift करता है।
  • Boxing एक value type को heap पर एक Object में wrap करता है; unboxing (CInt(o)) इसे वापस copy करता है।
  • Enum named choices का एक fixed menu define करता है: PayMode.Cash, .Card, .UPI।
  • Conversion family: CInt, CDbl, CStr, CBool, CDate, CType; CInt ROUNDS करता है (Java का cast chop करता था)।
  • Val leading digits parse करता है और माफ़ करता है; CInt एक fully numeric string demand करता है, वरना 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