Showing posts with label Components of Class. Show all posts
Showing posts with label Components of Class. Show all posts

Monday, April 29, 2013

Constructors:

किसी object को declare करते समय उसे construct करते समय new keyword का use कर उसका नया instance create किया जाता है। जब किसी class के object को declare करते है तब new keyword object का नया instance बनाने के लिए प्रयोग होता है। Constructors ऐसे member functions हैं जो कि किसी भी नए object मे fields और properties को initialize करने के लिए use किए जाते हैं। जब object को create करने के लिए new keyword का use करते है तब यह automatically call होकर properties को initialize कर देते हैं।

किसी भी class मे multiple contructores को भी use किया जा सकता है। जिसमे एक default और कई parameterized contructors हो सकते हैं। 


Constructors new subroutine के द्वारा implement किए जाते हैं। यह constructor object के new instance create होने पर automatically call हो जाते हैं। constructor बनाने के लिए Class मे कहीं भी new subroutine को define कर किया जाता है। parameterized contructor के लिए उसमें arguments को भी define कर दिया जाता है। 


 



Public Class Circle


Public Sub New()



Radius = 0



End Sub



Public Sub New(ByVal R As Double)



Radius = R



End Sub



End Class



 


इस प्रकार object को declare करने पर constructor automentic call हो जाएगा और value initialize हो जाएगी। 



Dim c As New Circle(10)

Methods: 


Methods object द्वारा perform किए जाने वाले actions को represent करते हैं। methods functions को ही कहा जाता है। इन्हे किसी special task को perform करने के लिए use किया जाता है। यह किसी भी problem को devide कर उसे easy बना देते हैं। 



<Access modifier> Function <Function Name> ([arguments]) As <Return Type>





[Function’s statements]





Return <returning value>





End Function




Ex:




Public Function Area() As Double



Return math.pi * radius ^ 2



End Function

 Properties: 


    Properties class मे use होने वाली fields की तरह होती हैं। लेकिन इन्हे Get और Set property के द्वारा implement किया जाता है। जो की data को control कर input और output को कंट्रोल किया जाता है। इसमें data को set करते समय उसे verify भी किया जा सकता है। 


         Set Section data को property मे set करते समय use होता है। और Get Section data को property से retrieve करते समय invoke होता है। property मे value को pass करते समय उसे Set section मे verify किया जा सकता है। data के valid होने पर उसे local field मे store का देते है । इसी प्रकार data को get करने के लिए उसी variable के द्वारा value return कर दी जाती है। जैसे previous example मे Radius एक property है। property को निम्न प्रकार से declare करते हैं। 


Syntax:



<access_specifier> [Readonly] property <property_name> () As <Type>



Get



[Statements]



End Get



Set (Byval Value As <Type>)



[Statements]



End Set



End Property




Exp:



Public Property Radius() As Double



Get



radius = cradius



End Get



Set(ByVal value As Double)



If value < 0 Then



msgbox("Wrong Input:")



Else



cradius = value



End If



End Set



End Property

Fields: 


Classes fields, properties, methods और events से मिलकर बनती है। यह सभी object information को contain करती है। Fields class मे use होने वाले variables को कहते है। जिसमें value को directly read और write किया जा सकता है। Fields को normal variables की तरह ही declare किया जाता है। इन्हे member variables भी कहते हैं जो कि data को handle करने कि लिए किया जाता है। Previous program मे cradius field है। 



Private cradius As Double

Contact us

Name

Email *

Message *