Theory
Screen की कोई Memory नहीं है
Unit 3 ने एक सुंदर till बनाई, पर देखिए data कहाँ रहता है: txtItem.Text, txtQty.Text, grid rows में बिखरा हुआ। Program में कोई THING नहीं है जो एक soap bar HO, अपने name और price के साथ।
BCA403 ने आपको cure सिखाई थी: असली चीज़ को एक class की तरह model कीजिए। ShopKeeper को एक Item चाहिए, फिर एक Bill।
VB.NET की classes वही hold करती हैं जो Java की करती थीं (fields, constructors, methods) plus 2 members जो Java के पास syntax की तरह कभी नहीं थे: Properties और Events। दोनों यही lesson हैं।
Practical
Item Class, सारे Members एक साथ
Public Class Item
Public Name As String ' field
Private _price As Decimal ' backing field, hidden
Public Sub New(ByVal name As String, ByVal price As Decimal)
Me.Name = name ' constructor: Sub New
_price = price
End Sub
Public Property Price As Decimal ' the property
Get
Return _price
End Get
Set(value As Decimal)
If value >= 0 Then _price = value ' guard the till
End Set
End Property
Public Function LineTotal(ByVal qty As Integer) As Decimal
Return _price * qty ' method
End Function
End Class
' At the till:
' Dim soap As New Item("Soap", 40D)
' soap.Price = 35D ' Set runs: accepted
' soap.Price = -5D ' Set runs: guard refuses, still 35
' MsgBox(soap.LineTotal(3)) ' 105
Theory
Construction और (Non-)Destruction
Construction: constructor एक Sub है जिसका नाम exactly New है। Dim soap As New Item("Soap", 40D) object allocate करता है और Sub New run करता है। इसे किसी भी procedure की तरह overload कीजिए; और BCA403 का vanishing-default rule यहाँ भी लागू होता है: कोई भी parameters वाला Sub New लिखिए और free no-arg वाला चला जाता है।
Destruction: आपको कुछ call नहीं करना। Garbage collector unreferenced objects reclaim करता है, और इससे पहले एक Protected Overrides Sub Finalize run कर सकता है, अपनी ही choice के time पर। Exam phrase: .NET में destruction non-deterministic है।
Theory
Properties: Guarded Doorway
Property को दोनों sides से देखिए।
बाहर से, यह एक field जैसा पढ़ता है: soap.Price = 35D, Dim p = soap.Price। कोई brackets नहीं, कोई method names नहीं।
अंदर से, हर read Get run करता है और हर write Set(value) run करता है, जहाँ आपकी logic guard खड़ी होती है: हमारी Set negatives को refuse करती है, तो soap.Price = -5D 35 को जगह पर छोड़ देता है।
Java ने आपको getPrice()/setPrice() लिखवाया और callers को इन्हें इस्तेमाल करना याद रखवाया। VB doorway को syntax में बेक कर देता है। No-logic cases के लिए, एक line काफ़ी है: Public Property Name As String (एक auto-property)।
Quiz
Listing के guard के साथ, soap.Price = 35D के बाद soap.Price = -5D के बाद इसमें क्या hold होता है?
- -5: आख़िरी assignment हमेशा जीतता है
- 35: Set block चला, और इसके guard ने negative value refuse कर दी
- 0: rejected assignments backing field reset कर देती हैं
- Compile error: आप एक property को assign नहीं कर सकते
Show the answer
35: Set block चला, और इसके guard ने negative value refuse कर दी
एक property को assignment इसके Set block की एक CALL है, value = -5 के साथ; If ने कुछ न करने का choice किया, तो _price ने 35 रखा। यह interception properties का पूरा point है: option A raw public fields के बारे में true है, यही exactly वजह है कि _price Private है, property इसके सामने खड़ी होकर। Option C ऐसी punishment logic invent करता है जो किसी ने नहीं लिखी। Option D backwards है: control के साथ assignability ही feature है। बाहर field-जैसा, अंदर method-जैसा: sentence के दोनों halves hold कीजिए।
Think first
Public Price As Decimal ही क्यों नहीं?
एक classmate कहता है: ceremony skip कीजिए, field को public बना दीजिए, 8 lines बचाइए। Tap करने से पहले 2 concrete reasons दीजिए कि property अपनी lines क्यों कमाती है।
Show the answer
1: Validation का एक address है। एक public field program की किसी भी line से -5 accept करता है; property का Set वह ONE gate है जिससे हर write गुज़रता है, तो no-negative-prices पूरी तरह enforceable बन जाता है। 2: आप outside तोड़े बिना inside बदल सकते हैं। अगर price बाद में एक discount rule या database read से आए, Get का body बदलता है और हर caller बिना छुए compile होता रहता है; एक public field के साथ, वह migration पूरे program को rewrite करती। यही encapsulation है (2 lessons आगे) अपने VB uniform में: वही argument जो आपने BCA403 में private + getters के लिए दिया था, बस clunky method calls के बिना।
Theory
Events: आप इन्हें पूरी Unit से Use कर रहे हैं
Unit 3 में आपने जो हर Handles btnAdd.Click लिखा वह आप किसी और के declare किए event को SUBSCRIBE कर रहे थे। जो classes आप लिखते हैं वे अपने खुद के declare कर सकती हैं:
Public Event LowStock(ByVal itemName As String)
और सही moment पर इसे fire कर सकती हैं:
If qty < 5 Then RaiseEvent LowStock(Name)
कोई भी form फिर इसे handle कर सकता है (AddHandler इसे code में wire करता है) और, मान लीजिए, एक reorder label flash कर सकता है। Event से declare कीजिए, RaiseEvent से fire कीजिए, Handles/AddHandler से सुनिए: Button कभी magic नहीं था, बस यही pattern Toolbox में ship हुआ था।
Watch out
तीन Member Mix-Ups
Sub New ही एकमात्र constructor spelling है: class Item के अंदर Item नाम का एक Sub एक ordinary method है, BCA403 के void Book() जैसा वही imposter trap।
Set refuse करते हुए भी run होती है: silence failure नहीं है; अपनी assumption नहीं, backing field check कीजिए।
Finalize पर भरोसा मत कीजिए: यह late run हो सकता है या (process exit पर) effectively बिल्कुल नहीं; जो कुछ भी promptly बंद होना ज़रूरी है (files, connections: Unit 5) उसे एक explicit close call चाहिए, कोई hope नहीं।
Theory
Bill Objects बन जाता है
ShopKeeper का redesign अब खुद लिखता है: एक Item class (यह listing), Items की एक ArrayList hold करने वाली एक Bill class जिसमें AddItem method और एक Total function है, और DataGrid OBJECTS जो कहते हैं वह दिखाती है, इसके बजाय कि यह अकेली जगह हो जहाँ truth रहती है। Screens display करते हैं; classes जानती हैं। अगले 3 lessons class machinery को tune करते हैं: कौन क्या touch कर सकता है (access specifiers), और family keywords Me, MyBase, MyClass जहाँ Java ने this और super कहा था।
Summary
Key takeaways
- Class ... End Class fields, Sub New constructors, methods, properties और events pack करती है।
- Construction: Dim x As New Item(...) Sub New run करता है; एक define करना free no-arg constructor हटा देता है।
- कोई destructors नहीं: GC objects reclaim करता है; Finalize run हो सकता है, non-deterministically।
- Property = Private backing field + Get/Set doorway: बाहर field syntax, अंदर guard logic।
- एक refuse करने वाली Set पुरानी value रखती है: 40, फिर 35, फिर -5 refused 35 रखता है; LineTotal(3) = 105।
- Events: Event से declare कीजिए, RaiseEvent से fire कीजिए, Handles/AddHandler से subscribe कीजिए: Unit 3 का Click demystified।
- Memory hook: properties एक doorman वाले fields हैं।