Theory
एक Bill पर कितने Items Fit होते हैं?
ShopKeeper का पहला bill design एक array इस्तेमाल करता है: एक purchase के items के लिए space।
Dim items(9) As String
Customer number 3, 11 चीज़ें खरीदता है।
आप यह wall पहले दो बार मिल चुके हैं: BCA104 के C arrays, और BCA403 का Unit 5 hook, जहाँ इसने हमें linked lists बनाने की तरफ़ push किया था। VB.NET आज 2 gentler answers देता है: एक array जो re-dimension हो सके, और एक collection जो खुद बढ़े। पर पहले, एक VB array surprise जो हर Java-trained eye को पकड़ता है।
Theory
Upper Bound से Declared, Count से नहीं
इस line को fresh eyes से पढ़िए:
Dim prices(4) As Double
C या Java में, 4 का मतलब 4 elements होता। VB.NET में यह सबसे highest index है: array prices(0) से prices(4) तक चलता है, जो 5 elements है, और prices.Length 5 कहता है।
Access parentheses इस्तेमाल करता है, brackets नहीं: prices(0) = 40। दोनों facts pure syntax हैं, और लगभग हर BCA404 paper में हर एक एक mark लायक है।
Practical
Arrays, ReDim, और Self-Growing ArrayList
Imports System.Collections
Module BillDemo
Sub Main()
Dim prices(2) As Double ' indexes 0..2: THREE slots
prices(0) = 40 : prices(1) = 100 : prices(2) = 60
MsgBox(prices.Length) ' 3
ReDim Preserve prices(4) ' grow to 5 slots, KEEP the 3 values
prices(3) = 25
Dim items As New ArrayList() ' grows by itself
items.Add("Soap")
items.Add("Tea")
items.Add("Rice")
items.Remove("Tea")
MsgBox(items.Count) ' 2
For Each thing As Object In items
MsgBox(CStr(thing)) ' Soap, then Rice
Next
End Sub
End Module
Quiz
Dim qty(4) As Integer. यह array कितने elements hold करता है?
- 4, indexes 1 से 4
- 4, indexes 0 से 3
- 5, indexes 0 से 4
- यह Option Strict पर depend करता है
Show the answer
5, indexes 0 से 4
VB.NET arrays को UPPER BOUND से declare करता है: qty 0 से 4 तक चलता है, पाँच elements, Length 5। Option B same line की Java/C reading है, exactly वह habit जो यह question ढूँढ रहा है: उन languages में new int[4] का मतलब 4 elements था। Option A 1-based arrays invent करता है, जो VB.NET के पास नहीं हैं (इसके arrays 0 से शुरू होते हैं, सिर्फ़ इसके classic STRING functions 1 से गिनते हैं: पिछला lesson)। Option D एक unrelated compiler setting घसीट लाता है; Option Strict conversions govern करता है, array sizing कभी नहीं।
Think first
ReDim बनाम ReDim Preserve
Bill array में 40, 100, 60 है। एक student ReDim prices(4) लिखता है, दूसरा ReDim Preserve prices(4)। Tap करने से पहले: इसके बाद हर array में क्या होता है?
Show the answer
Plain ReDim prices(4) एक fresh 5-slot array allocate करता है: सब zeros; 40, 100, 60 GONE हैं। ReDim Preserve prices(4) 5 slots allocate करता है और पुरानी values को across copy करता है: 40, 100, 60, 0, 0। एक keyword, पूरा bill saved या wiped। Preserve एक full copy की cost रखता है, तो एक loop में एक बार में एक slot बढ़ाना wasteful है, और वह hint (repeated unpredictable growth) exactly वह signal है arrays resize करना रोककर इसके बजाय एक collection इस्तेमाल करने का।
Theory
ArrayList: वह Bag जो बढ़ता है
Dim items As New ArrayList() (System.Collections से) को कभी size नहीं चाहिए:
items.Add("Soap")append करता है;items.Remove("Tea")value से delete करता है;items.RemoveAt(0)position सेitems.Countlive size है;items(0)index से पढ़ता है
एक honest cost: ArrayList सब कुछ As Object store करता है। Strings references की तरह slide हो जाते हैं, पर एक Integer add करना इसे box करता है (boxing वाला lesson, अपना cheque cash कराते हुए), और इसे वापस पढ़ने के लिए CInt चाहिए। Typed modern cousin, List(Of Double), boxing skip करता है और compile time पर ग़लत types को refuse करता है।
Watch out
Avoid करने के लिए तीन Array Slips
Brackets: prices[0] VB नहीं है; सिर्फ़ parentheses। (Confusingly, same parentheses functions भी call करते हैं: context decide करता है।)
End के आगे: prices(5) एक (4)-declared array पर run time पर IndexOutOfRangeException throw करता है; compiler आपको नहीं बचाता।
एक keep-the-data situation में Plain ReDim: silent, compile होता है, array wipe करता है। अगर values matter करती हैं, Preserve शब्द matter करता है।
Theory
कौन सा Container, कब
एक shopkeeper की तरह decide कीजिए: fixed rack sizes जो आप पहले से जानते हैं (7 weekday totals) एक array के लिए suit करते हैं; एक bill जिसकी length customer पर depend करती है एक ArrayList (या List(Of T)) के लिए suit करता है। ShopKeeper का bill दूसरे तरह का है, और Unit 3 से आगे इसके items एक collection में रहेंगे जो एक DataGrid feed करती है। और BCA403 का Unit 5 याद रखिए: linked lists ने यही problem BY HAND solve किया था; collections वही idea हैं, pre-built और shelf-bought।
Summary
Key takeaways
- Dim a(4) As Integer UPPER BOUND declare करता है: indexes 0..4, 5 elements, Length 5।
- VB arrays elements के लिए parentheses इस्तेमाल करते हैं: a(0); out-of-range access run time पर fail होता है।
- ReDim resize करता है पर wipe करता है; ReDim Preserve resize करता है और data रखता है (एक copy की cost पर)।
- ArrayList खुद बढ़ता और घटता है: Add, Remove, RemoveAt, Count।
- ArrayList Objects store करता है: value types box हो जाते हैं; List(Of T) typed, box-free alternative है।
- For Each arrays और collections दोनों पर चलता है।
- Memory hook: brackets में number top index है, head-count नहीं।