What is UBound and LBound in Array in Hindi
Makhanlal Chaturvedi University / BCA / VBA programming
UBound and LBound in Array in Hindi
UBound and LBound in Array in Hindi
Introduction
जब हम VBA (Visual Basic for Applications) में Array का उपयोग करते हैं, तो हमें यह जानने की आवश्यकता होती है कि उस Array की शुरुआत और अंत कहां होता है। इसके लिए VBA हमें दो बहुत ही उपयोगी Functions देता है: UBound और LBound। ये दोनों Function हमें यह जानकारी देते हैं कि Array की शुरुआत (Lower Bound) और समाप्ति (Upper Bound) किस Index पर है।
इस पूरी जानकारी को हम बहुत ही सरल और शिक्षक की तरह भाषा में समझेंगे ताकि कोई भी Beginner आसानी से इसे सीख सके। इस लेख में हम इन दोनों Function का उपयोग, लाभ, और practical examples के साथ विस्तार से चर्चा करेंगे।
What is LBound in VBA (LBound का मतलब क्या है?)
- LBound का पूरा नाम है "Lower Bound"।
- यह Function हमें यह बताता है कि किसी Array का सबसे छोटा Index कौन सा है।
- ज्यादातर मामलों में, VBA में Array की शुरुआत Index 0 से होती है, लेकिन कुछ खास settings में यह 1 भी हो सकता है।
What is UBound in VBA (UBound का मतलब क्या है?)
- UBound का पूरा नाम है "Upper Bound"।
- यह Function हमें बताता है कि किसी Array का आखिरी Index कौन सा है।
- यह जानना बहुत जरूरी होता है जब हम किसी Array पर Loop लगाते हैं या उसकी Length पता करना चाहते हैं।
How to Get Array Size using UBound Function in Hindi
Array की कुल size (यानि उसमें कितने elements हैं) जानने के लिए हम UBound और LBound दोनों का इस्तेमाल करते हैं। Size निकालने का formula नीचे दिया गया है:
Dim myArray(2 To 10) As Integer
Dim size As Integer
size = UBound(myArray) - LBound(myArray) + 1
MsgBox "Array का size है: " & size
- यहां LBound 2 होगा और UBound 10।
- तो size = 10 - 2 + 1 = 9
ध्यान दें: कई बार हम सोचते हैं कि सिर्फ UBound ही Array का size देगा, लेकिन अगर LBound 0 नहीं है तो यह गलत size देगा। इसलिए दोनों functions का use करना सही तरीका है।
Using LBound and UBound in Loops with Array in Hindi
जब हम किसी Array पर Loop चलाना चाहते हैं जैसे For Loop, तो हमें यह पता होना चाहिए कि Loop कहां से शुरू और कहां तक जाना है। ऐसे में LBound और UBound हमारे लिए बहुत काम आते हैं।
Dim students(1 To 5) As String
students(1) = "Amit"
students(2) = "Neha"
students(3) = "Ravi"
students(4) = "Sita"
students(5) = "Manoj"
Dim i As Integer
For i = LBound(students) To UBound(students)
MsgBox students(i)
Next i
- यह Code students नाम के Array को पढ़ता है और सभी नामों को Message Box में दिखाता है।
- यह loop अपने आप सही Index से चलता है क्योंकि हमने
LBoundऔरUBoundका इस्तेमाल किया है।
Why LBound and UBound are Beginner-Friendly in VBA
- ये दोनों Functions हमें Array के बारे में पूरा control देते हैं।
- अगर हम future में Array की size को dynamic बनाते हैं, तो हमें हर बार loop को manually update नहीं करना पड़ता।
- Loop logic को automate करने में ये functions बहुत मदद करते हैं।
- Program को error-free और flexible बनाने में मदद करते हैं।
Benefits of UBound for Dynamic Code in VBA in Hindi
जब आप dynamic Array का उपयोग करते हैं, जिसका size run-time पर बदलता है, तो UBound function बहुत उपयोगी हो जाता है।
- Dynamic Array ऐसा Array होता है जिसकी size पहले से fix नहीं होती।
- हम इसे
ReDimkeyword से समय के अनुसार बदल सकते हैं।
Dim marks() As Integer
ReDim marks(1 To 3)
marks(1) = 45
marks(2) = 50
marks(3) = 60
MsgBox "Array का size है: " & UBound(marks) - LBound(marks) + 1
Dynamic code में फायदे:
- Code automatically Array की current size जान लेता है।
- हर बार manually बदलने की जरूरत नहीं पड़ती।
- कोई भी programmer आसानी से Code को समझ और maintain कर सकता है।
- Error आने की संभावना बहुत कम हो जाती है।
Common Mistakes while Using UBound and LBound
- सिर्फ UBound का use करना और LBound को ignore करना।
- Array को initialize किए बिना UBound का इस्तेमाल करना। इससे “Subscript out of range” error आ सकती है।
- Multidimensional Array में गलत dimension का UBound लेना।
Dim myArray(1 To 3, 1 To 2) As String
MsgBox UBound(myArray, 1) 'First dimension
MsgBox UBound(myArray, 2) 'Second dimension
यहां हमने बताया कि multidimensional Array में किस तरह हम dimension number देकर UBound और LBound use कर सकते हैं।
Difference Between LBound and UBound
| Aspect | LBound | UBound |
|---|---|---|
| Full Form | Lower Bound | Upper Bound |
| Purpose | Array की शुरुआत का Index बताना | Array के अंत का Index बताना |
| Usage in Loop | Loop की शुरुआत तय करता है | Loop की समाप्ति तय करता है |
| Required for size | हाँ | हाँ |
Best Practices for Using UBound and LBound
- हमेशा दोनों functions का इस्तेमाल करें, खासकर जब Array dynamic या custom index से शुरू हो।
- Loop में hardcoded value (जैसे 0 या 1 से शुरू करना) से बचें।
- अगर आप multidimensional Array use कर रहे हैं, तो proper dimension parameter जरूर दें।
- Error handling करें अगर Array empty हो या initialize नहीं हो।
Real-World Use Case: Student Marks Calculator
Dim marks() As Integer
ReDim marks(1 To 5)
marks(1) = 80
marks(2) = 90
marks(3) = 85
marks(4) = 75
marks(5) = 95
Dim i As Integer
Dim total As Integer
total = 0
For i = LBound(marks) To UBound(marks)
total = total + marks(i)
Next i
MsgBox "Total Marks: " & total
इस example में हमने UBound और LBound का उपयोग करके student marks का total निकाला है, जो एक practical और real-life example है।
FAQs
UBound(array) - LBound(array) + 1। इससे Array में मौजूद elements की सही संख्या पता चलती है।
For i = LBound(array) To UBound(array)
'your code here
Next i
इससे loop पूरी Array को cover करता है और किसी Index को miss नहीं करता।