1 / 49

Sub Procedures

Sub Procedures. Creating Sub procedures:- A sub procedure is a procedure that performs actions. Syntax: Private | Public |[ Static ] Sub name [ ( arglist ) ] [ statements ] [ End Sub ] using Public , Private , Sub procedures are public by default.

erik
Télécharger la présentation

Sub Procedures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sub Procedures Creating Sub procedures:- A sub procedure is a procedure that performs actions • Syntax: Private | Public |[Static] Subname [(arglist)] [statements][End Sub] • using Public, Private, Sub procedures are public by default. • If Static isn't used, the value of local variables is not preserved between calls. • OR • To add a new general procedure to a form • Step 1: Display the code window of the form • Step 2: Select ADD procedure fro Tools Menu • Step 3: Enter a name in the Add procedure dialog box.

  2. Passing variables to the procedures • When you pass a value to a procedure we may pass it byval or byref • The byval sends a copy of the arguments value to the [rocedure so that the procedure cannot alter the original value. • The byref sends a reference indicating where the value is stored in the memory allowing the called procedure to actually change the arguments original value.

  3. Call Statement • Transfers control to a Sub procedure, Function procedure . • Syntax • [Call] name [argumentlist] • We are not required to use the Call keyword when calling a procedure. • However, if we use the Call keyword to call a procedure that requires arguments, argumentlist must be enclosed in parentheses. • If we omit the Call keyword, we also must omit the parentheses around argumentlist. • If we use either Call syntax to call any intrinsic or user-defined function, the function's return value is discarded.

  4. Function Procedures • A function procedure may perform actions but it also returns a value to the point from which it is called.

  5. Some common VB functions • Left Function :Returns a Variant (String) containing a specified number of characters from the left side of a string. • Left(string, length) • Left(patkar,3) will return “pat”

  6. Mid Function • Returns a Variant (String) containing a specified number of characters from a string. • Mid(string, start[, length]) • Mid(“patkar”,2,3) will return “atk”

  7. Right Function • Returns a Variant (String) containing a specified number of characters from the right side of a string. • Right(string, length) • Right(“patkar”,4) will return “tkar”

  8. Len function • Returns a Long containing the number of characters in a string or the number of bytes required to store a variable. • Syntax • Len(string ) • Len(“patkar”) will return 6

  9. LCase Function • Returns a String that has been converted to lowercase. • The required stringargument is any valid string expression. If string contains Null, Null is returned.

  10. UCase Function • Returns a Variant (String) containing the specified string, converted to uppercase. • The required stringargument is any valid string expression. If string contains Null, Null is returned. • Only lowercase letters are converted to uppercase; all uppercase letters and nonletter characters remain unchanged.

  11. LTrim, RTrim, and Trim Functions • Returns a Variant (String) containing a copy of a specified string without leading spaces (LTrim), trailing spaces (RTrim), or both leading and trailing spaces (Trim). • LTrim(string) • RTrim(string) • Trim(string)

  12. Static Arrays • Dim arrayname(lower subscript to upper subscript) as datatype • dim strname(0 to 25) as string • Dim balance(10) as currency • Dim num(1 to 100) as integer • The DIM statement allocates storage for the specified number of elements and initialises each numeric variable to 0. • In case of string arrays each element is set to an empty string(Null character). • It is not compulsory to provide the lower subscript value. • If no value is set for the lower subscript the lowest subscript is zero.

  13. Dynamic Array • Arrays can be dimensioned with empty parenthesis. • Dim straddress() as string • An array dimensioned in this way is referred to as the dynamic array. • The number of elements in a dynamic array may change during program execution by using ReDIM statement.

  14. Multidimensional arrays • Dim arrayname (lower_limit to upper_ limit),(lower_limit to upper_limit) as datatype • Dim matrix(2,2) as integer • Dim matrix(0 to 2,0 to 2) as double

  15. ARRAY FUNCTION • VB provides another method of initialising an array that uses a variant variable. • We can declare the variable to be an array • Ans aasign a list of values to it with the array function. • Variable=array(value list) • Strname=array(“ani”,”mary”,”george”,”sam”)

  16. For Each...Next Statement • Repeats a group of statements for each element in an array or collection. • Syntax • ForEachelementIngroup[statements][Exit For][statements] • Next [element] • The For...Each block is entered if there is at least one element in group. • Once the loop has been entered, all the statements in the loop are executed for the first element in group. • If there are more elements in group, the statements in the loop continue to execute for each element. • When there are no more elements in group, the loop is exited • and execution continues with the statement following the Next statement.

  17. User Defined data Types • A user defined data type can be used to combine several heterogeneous fields together. • For example an employee data type may contain empl_name,emp_code,address,date_of_join,designation etc • The fields can be combined into a user defined data type using type and end type statements

  18. Type Statements • You can combine variables of several different types to create user-defined types (known as structs in the C programming language). • User-defined types are useful when you want to create a single variable that records several related pieces of information . • You create a user-defined type with the Type statement, which must be placed in the Declarations section of a module. • User-defined types can be declared as Private or Public with the appropriate keyword. For example: • Private Type MyDataType

  19. Type statement Example • Type student • Name as string • Address as string • Rollno as integer • End type • Dim s as student

  20. Object oriented Programming • Class • Object • Encapsulation • Polymorphism

  21. What is an object? • An object is a unique programming entity that has attributes to describe it (like adjectives in grammar) and • methods to retrieve/set attribute values (like verbs in grammar).

  22. Attributes • Programmers store an object’s data in attributes, also called properties. • Attributes provide us a way to describe an object, similar to adjectives in grammar. • We can read property values or change properties, assigning values.

  23. Car Number Model No. Of Wheels Engine No Color Chassis No Owner _name State_registered Fuel _Tank_capacity Fuel_level Mileage

  24. Methods • Whereas attributes describe an object, methods allow us to access object data. Methods are like verbs in grammar. • We can manipulate object data, stored in attributes, using methods. Consume_petrol() Input_data() Display_data() Cover_journey()

  25. Abstraction • One of the chief advantages of object-oriented programming is the idea that programmers can essentially focus on the “big picture” and ignore specific details regarding the inner-workings of an object. This concept is called abstraction. Car_no Fuel_tank_capacity Fuel_Level Mileage Display_data() Input_data()

  26. Encapsulation • Abstraction in OOP is closely related to a concept called encapsulation. • Data and the ways to get at that data are wrapped in a single package, a class. • The only way to access such data is through that package. • This idea translates to information hiding.

  27. Classes • How do programmers implement abstraction? • They use a programming structure called a class. • A class presents a blueprint of an object, its properties and its methods.

  28. Creating a new class • Define A new class module • Click project menu • Click on add class module • Click open • Give a new name to the class

  29. Defining the properties of the class • In the general declaration code window declare the module level variables. • These variables become the properties of the new class.

  30. Add property Procedures • In the class module window click on tools menu • Click on add procedure. • Select property procedure • Write the name of the procedure.

  31. Get Property Procedure • Declares the name, arguments, and Propertyprocedure, which gets the value of a property. • [Public | Private | PropertyGetname [(arglist)] [Astype] [statements] [name=expression] End Property

  32. Property Procedures • Visual Basic provides three kinds of property procedures, as described in the following table. • Procedure Purpose • Property Get: Returns the value of a property. • Property Let: Sets the value of a property. • Property Set: Sets the value of an object property (that is, a property that contains a reference to an object).

  33. Let Property Procedure • PropertyLetprocedure, assigns a value to a property. • [Public | Private | PropertyLetname([arglist,] value)[statements] [statements] • End property

  34. Instantiation • To create an object based on a class, we create an instance of that class. This process is called instantiation. • we also use a special method called a constructor method to create an instance of an object. • Dim s1 as new student

  35. Set Statement • A set statement is an assignment statement for the object variables. • The variable must already declared with Dim/public/private /static statement. • Syntax: • Set <object variable>=new<class name>/nothing • Private s1 as student • Set s1=new student • Set s1=nothing • Setting an object variable to nothing terminates the object and release the system resources.

  36. Object references • You can make your Visual Basic applications run faster by optimizing the way Visual Basic resolves object references. • The speed with which Visual Basic handles object references can be affected by: • Whether an object reference is early-bound or late-bound.

  37. Early binding • Object references are early-bound if they use object variables declared as variables of a specific class. • If Visual Basic can detect at compile time what object a property or method belongs to, it can resolve the reference to the object at compile time. • The compiled executable contains only the code to invoke the object's properties, methods, and events. • This is called early binding. • When you declare an object variable using the class that defines the object, the variable can only contain a reference to an object of that class. • Visual Basic can use early binding for any code that uses the variable. • Early binding dramatically reduces the time required to set or retrieve a property value, because the call overhead can be a significant part of the total time. • For method calls, the improvement depends on the amount of work the method does.

  38. Early binding example • Private s(1 to 10) as new student • Dim e1 as employee Private sub form_load() • Set e1=new employee • End sub

  39. Late binding • Object references are late-bound if they use object variables declared as variables of the generic Object class. • When you declare a variable As Object, Visual Basic cannot determine at compile time what sort of object reference the variable will contain. • In this situation, Visual Basic must use late binding— that is, Visual Basic must determine at run time whether or not that object will actually have the properties and methods you used in your code. • Object references that use early-bound variables usually run faster than those that use late-bound variables.

  40. Late binding example • Private s1 as object • Private sub form_load() • Set s1=new student • End sub

  41. Class_Initialise Events • The class Initialise event is triggered when the object is created. • Public sub class_initialise() • Set s1=new student • End sub

  42. Class_terminate event • Class terminate event occurs when an object goes out of scope or is terminated. • Public sub class_terminate() • Set s=nothing • End sub • This event makes sure that the memory allocated for the object is released to the system resources.

  43. Collections • A collection class holds reference for a series of objects created from the same or different classes. • In concept the collection contains the objects. • But actually a collection holds a reference to each of the objects called members of the collection. • A coolection can hold multiple objects for you which is similar to an array which contains multiple variables. • A collection object has ADD method,Remove method ,Item method and Count property

  44. Creating an unique key • Because a collection is a group of objects we need to include a unique key in the class Public Function assignkey() As String Static id As Integer id = cid + 1 assignid = Trim(Str(id)) End Function

  45. ADD method • Adds a member to a Collection object. • object.Add item,key, • Object: An object expression that evaluates to an object in the Applies To list. • Item: An expression of any type that specifies the member to add to the collection. • Key Optional.:A unique string expression that specifies a key string that can be used, instead of a positional index, to access a member of the collection.

  46. Remove method • Removes a member from a Collection object. • object.Remove index • Object: An object expression that evaluates to an object in the Applies To list.indexRequired. An expression • that specifies the position of a member of the collection. • If a numeric expression, index must be a number from 1 to the value of the collection's Countproperty. • If a string expression, index must correspond to the keyargument specified when the member referred to was added to the collection.

  47. Item method • Returns a specific member of a Collection object either by position or by key. • Syntax • object.Item(index)

More Related