Creating Classes, Object Construction & Destruction: Properties, Methods, Events

VB.NET class એ fields, Sub New construction, methods અને events ને pack કરે છે, અને Property block ને add કરે છે: બહાર field-like syntax, અંદર full Get/Set control, જ્યાં negative prices ને refused થવા જવું પડે છે.

12 min read · 10 cards · 2 checks

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


Theory

screen ને memory નથી

Unit 3 એ beautiful till build કર્યું, પણ look કરો કે data ક્યાં lives કરે છે: txtItem.Text, txtQty.Text, grid rows પર scattered. program માં કોઈ THING નથી જે IS soap bar છે તેના name અને price સાથે.

BCA403 એ તમને cure શીખવ્યું: real thing ને 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   ' till ને guard કરો
        End Set
    End Property

    Public Function LineTotal(ByVal qty As Integer) As Decimal
        Return _price * qty            ' method
    End Function
End Class

' 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 છે જેનું name exactly New છે. Dim soap As New Item("Soap", 40D) object ને allocate કરે છે અને Sub New ને run કરે છે. Overload કરો તેને કોઈ પણ procedure જેવું; અને BCA403 નો vanishing-default rule અહીં પણ apply થાય છે: parameters સાથે કોઈ પણ Sub New લખો અને free no-arg one ચાલ્યો જાય છે.

Destruction: call કરવા માટે કંઈ નથી. garbage collector unreferenced objects ને reclaim કરે છે, અને તે કરતા પહેલા Protected Overrides Sub Finalize ને run કરી શકે છે, તેના પોતાના choosing ના time પર. exam phrase: .NET માં destruction non-deterministic છે.

Theory

Properties: guarded doorway

property ને બંને sides થી look કરો.

બહાર થી, તે field જેવું reads થાય છે: soap.Price = 35D, Dim p = soap.Price. કોઈ brackets નહીં, કોઈ method names નહીં.

અંદર થી, દરેક read Get ને run કરે છે અને દરેક write Set(value) ને run કરે છે, જ્યાં તમારું logic guard તરીકે stands કરે છે: આપણું Set negatives ને refuse કરે છે, એટલે soap.Price = -5D 35 ને place માં leave કરે છે.

Java એ તમને getPrice()/setPrice() લખવા મજબૂર કર્યા હતા અને callers ને તેમને use કરવા યાદ રાખવા મજબૂર કર્યા હતા. VB doorway ને syntax માં bake કરે છે. no-logic cases માટે, one line suffice કરે છે: Public Property Name As String (auto-property).

Quiz

listing ના guard સાથે, soap.Price શું hold કરે છે after: soap.Price = 35D followed by soap.Price = -5D?

  1. -5: last assignment હંમેશા wins કરે છે
  2. 35: Set block ran, અને તેના guard એ negative value ને refuse કર્યું
  3. 0: rejected assignments backing field ને reset કરે છે
  4. Compile error: તમે property ને assign નથી કરી શકતા
Show the answer

35: Set block ran, અને તેના guard એ negative value ને refuse કર્યું

property ને assignment એ તેના Set block ને CALL છે value = -5 સાથે; If એ કંઈ કરવાનું choose ન કર્યું, એટલે _price એ 35 retain કર્યું. એ interception એ properties નો entire point છે: option A raw public fields માટે true છે, જે exactly એટલે છે કે _price Private છે property તેની front માં standing સાથે. Option C punishment logic ને invent કરે છે જે કોઈએ નથી લખ્યું. Option D backwards છે: control સાથે assignability એ feature છે. બહાર field-like, અંદર method-like: તે sentence ની બંને halves ને hold કરો.

Think first

શા માટે just Public Price As Decimal નહીં?

classmate કહે છે: ceremony ને skip કરો, field ને public બનાવો, 8 lines save કરો. 2 concrete reasons આપો કે property તેની lines ને earn કરે છે, tap કરતા પહેલા.

Show the answer

1: Validation ને address છે. public field એ program ની કોઈ પણ line થી -5 ને accept કરે છે; property નું Set એ ONE gate છે જે દરેક write pass કરે છે, એટલે no-negative-prices ને all પર enforce કરી શકાય છે. 2: તમે બહાર ને break કર્યા વગર અંદર ને change કરી શકો છો. જો price later discount rule અથવા database read થી આવે છે, Get નું body changes થાય છે અને દરેક caller untouched compile થાય છે; public field સાથે, તે migration આખા program ને rewrite કરે છે. આ encapsulation છે (2 lessons ahead) જે તેનું VB uniform પહેરે છે: same argument જે તમે BCA403 માં private + getters માટે made કર્યું હતું, minus clunky method calls.

Theory

Events: તમે આખું unit તેમને use કરી રહ્યા છો

દરેક Handles btnAdd.Click જે તમે Unit 3 માં લખ્યું હતું તે તમે SUBSCRIBING હતા event ને જે કોઈ બીજાએ declare કર્યું હતું. classes જે તમે write કરો છો તે તેમના own ને declare કરી શકે છે:

Public Event LowStock(ByVal itemName As String)

અને fire કરી શકે છે જ્યારે moment આવે છે:

If qty < 5 Then RaiseEvent LowStock(Name)

કોઈ પણ form તેને handle કરી શકે છે (AddHandler તેને code માં wires કરે છે) અને, say, reorder label ને flash કરે છે. Event સાથે declare કરો, RaiseEvent સાથે fire કરો, Handles/AddHandler સાથે listen કરો: Button ક્યારેય magic નહોતું, just આ pattern Toolbox માં shipped હતું.

Watch out

ત્રણ member mix-ups

Sub New એ only constructor spelling છે: class Item ની અંદર Item નામ નું Sub એ ordinary method છે, same imposter trap જે BCA403 નું void Book() હતું.

Set runs ત્યારે પણ જ્યારે તે refuse કરે: silence failure નથી; backing field ને check કરો, તમારા assumption નહીં.

Finalize પર count ન કરો: તે late run થઈ શકે છે અથવા (process exit પર) effectively not at all; જે કંઈ promptly close થવું જોઈએ (files, connections: Unit 5) તેને explicit close call મળે છે, hope નહીં.

Theory

bill objects બને છે

ShopKeeper નું redesign હવે itself writes છે: Item class (આ listing), Bill class જે Items ના ArrayList ને hold કરે છે AddItem method અને Total function સાથે, અને DataGrid જે OBJECTS કહે છે તેને display કરે છે, instead of being the only place truth lives. screens display કરે છે; classes know કરે છે. next 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.
  • Refusing Set old value ને leave કરે છે: 40, પછી 35, પછી -5 refused keeps 35; LineTotal(3) = 105.
  • Events: Event સાથે declare, RaiseEvent સાથે fire, Handles/AddHandler સાથે subscribe: Unit 3 નું Click demystified.
  • Memory hook: properties એ fields છે doorman સાથે.

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 Object Oriented Programming

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

Creating Classes, Object Construction & Destruction: Properties, Methods, Events · .NET Programming · Gri-Learn