Visual Basic .NET The Complete Reference phần 4 pptx

67 351 0
Visual Basic .NET The Complete Reference phần 4 pptx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

or values and take action accordingly. It can perform such checks in a number of ways, which includes specifying the type required, a built−in strong typing filter. The level of method coupling varies in the previous four key options for providing data to the method. The highest level of coupling between methods results from the class level or global variables, while the lowest level of coupling is achieved when data is explicitly sent to the method via its formal parameter list. There will be more information on method data later in this chapter. Method Data: Global vs. Local Methods perform their work with local data or class data. When you need to modify variables for other methods in the class or for operations that interest the entire class, then use class variables. If no other method requires access to the variable or needs to use it, then declare it local to the method. In my opinion, you should not declare class variables if you don't need to, because the narrower the scope of access on a variable, the more secure it is and the less coupled your method will be to other methods in the class. There is another school of thought, however, that says passing arguments to methods requires more resources from the run−time environment. I don't believe this is a valid argument (no pun intended) in the world of managed code and execution, because the common language runtime manages the call stacks with a very high degree of sophistication. Providing too many global variables eventually leads to problems. Another criterion for declaring class variables or constants is when the values need to remain intact after the method has performed its work. Local variables are created and initialized either to the default values of their types or values you provide when the method is invoked or called. The data and references are subsequently destroyed when method execution ends. As mentioned earlier, you can use a static variable in a method if you require value persistence after execution, but static variables are more resource−intensive than class−level variables, because static variables are stored on the heap as boxed values (see Chapter 8). Local variables are declared in either of two declaration spaces in a method: as a parameter in the formal parameter list space, or as a local variable in the method's local declaration space. The following illustrates the two declaration spaces we are talking about: Public Sub Space(Parameter lists declaration space) local declaration space End Sub The method's local declaration space can also mean local to the block of code within the method implementation. This means that you can declare at any level as needed, because each nested block become its own declaration scope and context. This is demonstrated in the following code: Public Sub BirdsNest () Dim field1 As Integer = 1 Const field2 As Integer = 2 While field1 <= field2 Dim field3 As Integer = 3 Const field4 As Integer = 4 While field3 <= field4 field3 += 1 End While field1 += 1 End While End Sub Method Data: Global vs. Local 187 Note the following rules for declaring variables and constants in method block and parameter lists: The variable declared in the parameter list cannot be redeclared in any method block.• A variable declared in an outer or containing block cannot be redeclared in a nested block (or any other block for that matter). You cannot shadow method data, so consider the formal parameter list as the outermost declaration space before class data. • Method declarations are scoped to the block in the method in which they are declared.• Method variables and constants are implicitly declared public, and you cannot modify the access characteristics with keywords like Private or ReadOnly. • The Option Strict and Option Explicit compiler directives (refer to Chapter 4) influence variable and constant declarations. You can implicitly declare (by simply just using the variable) and use loose syntax by setting both options to Off for the class. This practice is, however, greatly discouraged because it causes buggy and hard to maintain code. (Refer to Chapter 4 for more information on implicit and loose syntax declarations.) There is an exception to this rule, however. A local method variable may not be implicitly declared when it is the target of a function call, an indexing expression, or a member access expression. Also, implicitly declared locals are always scoped to the entire method body. • When you declare implicitly, the type of the variable defaults to Object if no type character was attached to the implicit declaration; otherwise, the type of the variable is the type of the type character. In the following example you can see how it is possible to set yourself up for problems with implicit declaration. The return value will continue to be wrong no matter what gets passed to the method until someone realizes the simple spelling mistake: Public Function FactorLightSpeed(ByVal warp As Decimal)_ As Decimal speed = 186355 \ waro Return speed End Function • Variable initializers on method locals are equivalent to assignment statements placed at the textual location of the declaration. Thus, if execution branches over the local declaration, the variable initializer will not be executed. Also, if the method variable declaration is executed more than once, the variable initializer will be executed an equal number of times. It is important to note that locals are only initialized to their type's default value once, upon entry into the method (refer to "Local Declarations" in the next section). • Local Declarations Local, or method, declarations can store both constant data using the Const keyword, which is no different from a class constant declaration, or variable data. Variables can be declared using the Dim keyword or the Static keyword. The following syntax represents local declaration: LocalDeclarationStatement ::= LocalModifier LocalDeclarator StatementTerminator LocalModifier ::= Static | Dim | Const LocalDeclarator ::= LocalIdentifiers [ As TypeName ] Identifier [ ArrayNameModifier ] [ As [ New ] TypeName [ ( [ ArgumentList ] ) ] ] [ = VariableInitializer ] LocalIdentifiers ::= Identifier [ ArrayNameModifier ] | LocalIdentifiers, Identifier [ ArrayNameModifier ] LocalVariableName ::= Identifier Local Declarations 188 As the name suggests, constant data cannot be changed. It consists of read−only values, but the ReadOnly keyword is not valid in either of the method declaration spaces (see the "Properties" section later in this chapter). Static Data The Static keyword modifies a local variable declaration to static, which plays an important role in code reentrance, isolation, encapsulation, and recursion (see Chapters 4, 12, 13, and 14 and the later section "Recursive Design of Methods" in this chapter). When you declare static variables, their values are retained for future calls to the method. It is critical to be aware that declaring a static local in a shared method means that the value of the static is retained for all references to the type. In other words, there is only one copy of the static local at all times. Dependence on the data held by the static must therefore be carefully reviewed. Remember that static methods (which are declared with the modifier Shared in Visual Basic and static in C#) are not instance methods. For all intents and purposes, the method and the static data are both global entities. (See the section "Improved Performance with Shared Classes and Modules" in Chapter 9.) When you declare a static local in a nonshared method, which allows instantiation, then a separate copy of the static exists for each instance of the object, and the static's value is retained for the clients that have a reference on the object that encapsulates the static. The following code demonstrates declaring a static variable in a nonshared method (notice the use of Hungarian notation for clearly marking static variables): Private Function ChurnOut(ByVal Param As Integer) As Integer Static stChurnval As Integer ' End Function Returning with Values By default, all methods return to the caller or sender that called them. And as demonstrated in Chapter 6, you can use the Return keyword to terminate and exit out of a method at any point, even from a Sub method. In this regard Return works exactly like Exit Sub. However, when you declare a function, you are advising the parties concerned that a value will come back from the method being called, so you must supply a return value and that value must be the same type as the value declared as the return value variable. This is demonstrated as follows: Private Function ChurnIn(ByVal Param As Integer) As Integer ' do something with Param Return Param End Function The return value declared after the parameter list is a local variable declaration, just like the parameters and the variables declared in the body of the method. The function name is the name of the variable. For example, looking at the preceding method ChurnIn, you can see the variable declaration if you drop the parameter list as follows: Function ChurnIn As Integer To return ChurnIn as an Integer, you do not need to use Return unless there are several places in the function where return is possible (such as in a Select Case construct or a nested structure). However, if you do Local Declarations 189 use Return, you must supply the value returned. Here are the additional variations to returning the value: Private Function ChurnIn(ByVal Param As Integer) As Integer ' do something with Param and assign to ChurnIn 'ChurnIn returned implicitly ChurnIn = Sqrt(Param) End Function or Private Function ChurnIn(ByVal Param As Integer) As Integer ' do something with Param ChurnIn = Sqrt(Param) Return ChurnIn End Function or Private Function ChurnIn(ByVal Param As Integer) As Integer ' do something with Param Dim valuable As Integer valuable = Sqrt(Param) Return valuable End Function Note Chapter 12 investigates passing and receiving arrays from methods. Passing Arguments to Parameters As discussed earlier in this chapter, the parameters of a method are declared by the method's formal parameter list. (The parameter list and method name, or identifier, are combined to form the method's signature.) The parameter declarations of a method are the "placeholders" for the data sourced external to the method and the means by which the data can be "communicated" to the method for its use. For example, if a method needs to multiply two numbers represented by x and y, then you can communicate the value for x and the value for y by sending the respective values to the parameters. The "sender" of the values refers to these values as the arguments for the parameters. Parameters are the "receivers"; you can think of them as pigeonholes or slots into which the arriving data is channeled. It's important to get the difference between arguments and parameters right. Many programmers confuse the terms to their own detriment (parameters receive; arguments send). A method can receive a variety of types to its parameters: value types, reference types, and a collection (of types) that arrives as a comma−delimited list. The latter parameter is known as a parameter array. A fourth type of parameter is the "optional" parameter, which lets the sender choose not to send an argument. The method applies a default value to the optional parameter and then passes that into the method. Method processing can be very tightly controlled by the optional parameter, as you will see later in this chapter and in later chapters. The formal parameter keywords are listed in Table 7−1. Table 7−1: Parameter Types Excepted at Methods Passing Arguments to Parameters 190 Parameter Type Keyword Value Types ByVal Reference Types ByRef Optional Parameter Optional Array Parameter ParamArray You can specify multiple parameters in the parameter list; however, each parameter declaration must be separated by a comma so that the compiler can discern the bounds of each parameter. You can use loose and implicit syntax in the declaration of parameters (if Option Strict and Option Explicit are set to Off), but if you use the As keyword for one parameter declaration, you need to use it for all. If you use implicit syntax and do not declare the type of parameter, then the type defaults to Object. The syntax for declaring the parameter list is as follows: FormalParameterList ::= FormalParameter | FormalParameterList, FormalParameter FormalParameter ::= [ Attributes ] ParameterModifier+ Identifier [ As TypeName ] [ = ConstantExpression ] ParameterModifier ::= ByVal | ByRef | Optional | ParamArray Pass by Value Value parameters are passed by value and are explicitly declared with the ByVal modifier. If you omit the modifier in your code, the compiler automatically defaults the parameter acceptance mode to ByVal. The Value variable is created when the method is called, and is destroyed when the method execution ends (returns). The scope of the parameter's variables is the entire method, including any nested blocks. Here's an example: Public Sub ValueIn(ByVal Data As String) Console.WriteLine("Data received is: " & Data) End Sub You are permitted to change the value received to the parameter (that is, the value that was sent to the method as the argument). Remember, once the value is passed to the parameter, changing the value in the method does not affect the source of the original datathe argument sent is a copy of the data, not a reference to it. As mentioned earlier, the parameter list is a declaration context, and the argument merely serves to initialize it with the value it carries to the method. This is demonstrated in the following example: Public Function ValueIn(ByVal Number As Integer) As Integer Number += 5 If (Number > 10) Return Number End If Return 0 End Function Passing Arguments to Parameters 191 Pass by Reference When you pass by reference, the data and construct represented by reference are directly affected by the operations on the parameter. Passing by reference means you do not make a copy of the value and pass that to the method. No new storage location is created to hold any data. Think of the pass by reference as the baton in a relay race. There is only one baton being passed and if it gets dropped the passer gets disqualified. You can pass both value types and reference types using the ByRef keyword. Passing by reference will become more clear in the next chapter and Chapter 9, which delve into the object reference models. When you pass a value by reference, you are essentially requesting the method to directly and immediately act on the original data. The following example demonstrates passing a value type by reference: Public Sub ChangeUp(ByRef myVal As Integer) ChangeUp(myVal) End Sub Despite the declaration of a reference parameter, Visual Basic .NET may still use copy−in/copy−out semantics when a reference variable is passed to a ByRef parameter. This usually happens when there is either no storage location to pass a reference to, which is what happens when the argument references a property, or when the type of the storage location is not the same as the parameter type's. The latter situation happens, for example, when you pass an instance of a parent class to a ByRef derived class parametera technique called upcasting. Thus, a reference parameter may not necessarily contain a reference to the exact storage location of the object, and any changes to the reference parameter may not be reflected in the variable until the method exits. Optional Parameters Optional parameters can be passed to methods. An optional parameter is declared with the Optional modifier. Parameters that follow an optional parameter in the formal parameter list must be optional as well or you will choke the compiler. Thus, if you have numerous parameters in the parameter list, make sure all the optional ones are in their specific order and are placed after the other three parameter types. The following code provides an example: Function DDB(ByVal rcost As Double, _ ByVal rsalvage As Double, ByVal rlife As Integer, _ ByVal rperiod As Integer, _ Optional ByVal rfactor As Decimal = 2) As Double Dim book As Double = cost salvage Dim deprec As Double Dim year As Integer = period While year > 1 deprec = book * factor / life book = book deprec year −= 1 End While Return book End Function Optional parameters must specify constant expressions to be used as the default value if no argument is specified, as demonstrated in the bold type in the preceding code. This is the only situation in which an initializer on a parameter is valid. The initialization is always done as a part of the invocation expression, not within the method body itself. Also, optional parameters may not be specified in Delegate or Event Passing Arguments to Parameters 192 declarations. Passing a Parameter Array A parameter array is a single dimension data structure that can be passed as an argument to a parameter. The differences between passing arguments to a parameter array and passing a regular array reference as the argument are as follows: The ParamArray parameter expects the argument as a value and not as a reference.• You can only declare one ParamArray in the parameter list, while you can declare more than one parameter that expects a reference to a regular array or collection. • The ParamArray parameter is useful for sending on−the−fly lists of data to the method, without the need to specially pass over a reference to any collection. It is ideal for varying the number of arguments needed by the method. This is demonstrated in the following code: Module ParamArrayTest Sub Main() Dim arrayA As Double() = {1, 4, 2.5, 3.9} GetArrayVals(arrayA) GetArrayVals(10.6, 20, 30.0, 40.87, 987.3) GetArrayVals() Console.ReadLine() End Sub Sub GetArrayVals(ByVal ParamArray arrayargs As Double()) Debug.Write(arrayargs.Length & " elements passed:") Dim intI As Integer For Each intI In arrayargs Debug.Write(" " & String.Format("{0:c}", intI)) Next intI End Sub End Module • This example produces the following output: 4 elements passed: $1.00 $4.00 $2.00 $4.00 5 elements passed: $11.00 $20.00 $30.00 $41.00 $987.00 0 elements passed: In the above code the first call simply passes a pre−packaged array to the parameter. The second call creates a five member comma−delimited list and passes the list. The third method call demonstrates that you can pass zero elements to the ParamArray parameter. When you are passing a simple list to the ParamArray parameter it is often difficult to distinguish between the list of values and values in regular arguments because the argument list is not distinguishable from the list intended for the parameter array. While the compiler can make the distinction because it knows where the ParamArray starts in the receiving method, you may see the subtle "bug" when you make the call. For example, can you tell that the following call is actually sending two arguments? GetVals(1, 10.6, 20, 30.0, 40.87, 987.3) You can now if you examine the method signature as follows: Sub GetVals(ByVal intI As Integer,_ ByVal ParamArray pArgs As Double()) Passing Arguments to Parameters 193 You can avoid the problem by first avoiding hard−coding and doing away with magic numbers and arbitrary values in your code as shown in the following call: GetVals(MyDayEnum.Sunday, MoneyToParamArray) To send data to a parameter array your need to declare the parameter with the ParamArray modifier. And you cannot declare a parameter of type ParamArray without specifying the ByVal (ByRef is invalid). Like the optional parameter discussed earlier the paramarray parameter must be the last parameter in the formal parameter list. Unlike the optional parameter you do not have to provide default values. If the sender does not send an argument to the parameter array the array will default to an empty array. Parameter array usage is as follows: The parameter array will perform a widening conversion on the argument if the argument is narrower than the parameter. However, if the argument is wider than the parameter array or incompatible an exception will be thrown. • The sender can specify zero or more arguments for the parameter array as a comma−delimited list, where each argument is an option for a type that is implicitly convertible to the element type of the paramarray. An interesting activity takes place on the call. The caller creates an instance of the paramarray type with a length corresponding to the number of arguments, initializes the elements of the array instance with the given argument values, and uses the newly created array instance as the actual argument to give to the parameter. • Paramarray parameters may not be specified in delegate or event declarations.• Parameter arrays are useful and you don't need to treat the parameter any different, from inside your method, as you do the regular array reference. Calling Methods Methods are called (or invoked) via an interface, which is accessed by referencing the class or object containing the method, followed by a reference to the method's signaturewhich is its name and (mostly) any arguments of the type and order, in the target method's parameter list. When you reference a method, you invoke it or call it. Some OOP experts also refer to the invocation of the method as "sending a message to the method." And thus the term sender is frequently used, especially in event models. Conversely, the method on the receiving end of the message or call is known as the receiver. From time to time, we will refer to the construction of our code by the particular method calls that have to be made. Later in this chapter, we will see how Visual Basic methods can call themselves (recursion). Call by Reference or Call by Value As noted in Table 7−1, arguments can be passed to parameters by value or by reference. When you pass by value, you are passing the actual value to the method (some prefer to say "call by value"). When you pass by reference, you are passing only a reference to an object. Value typessuch as the built−in Integer, Short, Single, and Doubleare passed by value. Reference typessuch as arrays, strings, and custom objectsare typically passed by reference. Suppose the method you are calling needs to receive an array. You don't need to send the array to the method (although this was once the case some time ago) but rather a reference to the array. This means the object stays exactly where it is and the method can still go to work on the array. This will become clearer to you in Calling Methods 194 Chapter 8, in the "Object Reference Model" section, which covers both how value types and reference types are references, and in the method design and construction sections later in this chapter. The same procedurepassing the reference and not a copy of the valueapplies when receiving the return value back from the function. If the function returns a value type, you'll receive a copy of the value; but if it returns a reference type, such as an array, you'll only get the reference to the array from the function. Passing the actual value to the receiving method does not change any data that exists in the calling method or elsewhere, because a copy of the value is sent to the receiver. The value copy is then used and discarded. However, when you pass by reference, any changes made to the object by the receiving method affect the original object, because you don't send a copy of the object, you send a "message" telling the calling method where to find the object that needs to be worked on. The procedure is akin to sending someone the keys to a house, rather than the house itself. For example, when you send a reference to an array that needs to be sorted, all entities referencing the array will see the results of the sort. This is an important consideration to keep in mind when designing your software. For the most part when you call a method, you do not need to worry about whether you are passing by reference or passing by value. Visual Basic enforces the calling procedure and knows whether it should pass by value or by reference (see Chapter 12 for information on passing and receiving arrays). The following line of code is a typical method call found in many Visual Basic applications. Notice that this method does not pass any arguments. Beep() All this method does is make a call to the Beep functionwhich beeps the speaker in the class maintained in the Microsoft.VisualBasic namespace. (The Microsoft.VisualBasic represents the classic VB runtime and its collection of types, libraries, and run−time constructs that provide the interface to the Win32 library for the classic Visual Basic languages. Any .NET language can now reference it thanks to the framework's interop layers and wrapper classes that adapt legacy code for access from the world of .NET.) The fully qualified namespace for the method is as follows: Microsoft.VisualBasic.Interaction.Beep In other words, the Beep method can be found in the Interaction class contained in the Microsoft.VisualBasic namespace. Table 7−2 provides the entire list of legacy function calls you can access in this "wrapped" class. Table 7−2: Legacy Functions in the Microsoft.VisualBasic.Interaction Class Function Purpose AppActivate Launches an executable (see also Shell) Beep Standard speaker beep CallByName Sets and gets properties and invokes methods at run time by providing arguments to the CallByName method Choose Selects and returns values from a parameter list Calling Methods 195 Command Returns the argument portion of the command line used to launch VB apps CreateObject Creates a reference to a COM object DeleteSetting Deletes Registry settings Environ Returns the OS environment variables as a string GetAllSettings Returns all the settings related to an application's Registry settings GetObject Returns a reference to an object provided by a COM object GetSetting Returns a key setting from the Registry Iif Returns one of two objects passed as arguments and tested against a Boolean key InputBox Creates a dialog box for user input and then returns the input to the caller MsgBox Display a dialog box, presents options for the user, and then returns an Integer based on the choice of the user Partition Calculates a set of numeric ranges SaveSetting Saves an application's Registry setting Shell Runs an application and returns its process ID from the OS Switch Evaluates a list of expressions Function or Sub Methods In all modern computer languages, methods can be constructed to return with or without a value to a caller. As mentioned at the start of this chapter, the methods that return a value are called functions and the methods that don't return a value are called subroutines or subprocedures. Visual Basic methods that return without values are declared with the Sub keyword. Consider the Beep method we looked at previously. If you were to open up the Interaction class and search for the Beep method, you would find the following definition: Public Sub Beep() Public refers to the accessibility of this method (you can call it from anywhere). It is referred to as a Sub procedure because it does not return with a value. Note The .Net Framework documentation refers to the Beep procedure as a function, which is technically wrong. Technically, it is not a function because it does not return any value to the caller. But the VB runtime calls it a function, even though the .NET code defines it as a Sub. You will find many such inconsistencies in the documentation of various languages, .NET included. I even traded insults with another writer over the term method when first embarking on Visual Basic .NET. The following line of code is an example of a function call: Public Function Choose(Index As Integer, ParamArray Choice() _ As Object) As Object Choose is another classic VB function, and in this declaration, the method requires you to send an argument of type Integer for the Index parameter and the Choice array object for the ParamArray parameter. The returning type for this function is declared at the end of the method declaration; in this case, the return type is Object. Function or Sub Methods 196 [...]... the square root of a specified number Tan Provides the tangent of the specified angle Tanh Provides the hyperbolic tangent of the specified angle To investigate the constants and methods (and other members) of the Math class, open the Object Browser in Visual Studio The easiest way to do this is to use the keyboard shortcut CTRL−ALT−J The browser can also be accessed from the menus: Select View, Other... method The steps must be performed in the order listed in the method, which together logically make up the entire process A good example is a method that opens a file, writes to the file, and then closes the file While you can easily transfer control to another method, it makes no sense to not complete the steps in a single method On the other hand, if you were to insert a step that reads the data in the. .. an object in the parameter array at the index position, 1 or higher, expressed in the Index parameter In other words, if the argument to Index is "1" then the first item in the parameter array is returned to the caller You would invoke this method as follows: Choose(num, "red", "white", "blue") If num in the preceding call is 2, then "white" is returned to the caller Digging around in the VB runtime... In the ApplySalesTax method, the parameter tax is the percentage value to apply to the vector value Without the parameter, the method is useless The final and essential section of the method spec is the documentation of the return values, conditions (also known as postconditions), return codes, and so on from the method Future programmers (or concurrent members of the team) will be able to grasp the. .. field is 3. 141 59265358979323 846 C = π*D Description Provides the absolute value of a number Provides the angle whose cosine is the specified number Provides the angle whose sine is the specified number Provides the angle whose tangent is the specified number Provides the angle whose tangent is the quotient of two specified numbers Provides the smallest whole number greater than or equal to the specified... specified number Provides the cosine of the specified angle Provides the hyperbolic cosine of the specified angle Provides e raised to the specified power Provides the largest whole number less than or equal to the specified number Provides the remainder resulting from the division of a specified number by another specified number Provides the logarithm of a specified number Provides the base 10 logarithm... part of the argument is itself a method call (under the hood, however, the return value from Choose represents the argument to the Write method) Table 7−3 lists the legacy functions available to us in the Microsoft.VisualBasic VbMath class If it is not clear to you by now, programming against the NET Framework consists of calling or invoking the thousands of methods that have been provided in the Framework... Math Expand the class and the complete list of members will be loaded in the right pane in the Object Browser Every method is documented, as illustrated in Figure 7−2 207 Programming with the Math Class Figure 7−2: Browsing the members of the Math class Programming with the Math Class The following two examples, not by any means significant algorithms, demonstrate calling the methods in the Math class... use the methods and other members of the class, you need to first reference the class via its namespace This can be done using the Imports directive, as demonstrated in Chapter 4, the Project Imports folder that can be set in a project's Property Pages dialog box, or the following line of code: X = System.Math.Sin(Y) First let's have a look at the Pow method (power), which returns the number of the. .. in either the Get block or the Set block or both, anything declared in the Set block is not visible to the Get block If the property goes to work a variable that has not been declared or that it cannot see, it will cause a StackOverflowException to be raised You also need to provide the typing specifics for both the return value and the parameters of the property This you do with the As syntax on the . that the value of the static is retained for all references to the type. In other words, there is only one copy of the static local at all times. Dependence on the data held by the static must therefore. method does is make a call to the Beep functionwhich beeps the speaker in the class maintained in the Microsoft.VisualBasic namespace. (The Microsoft.VisualBasic represents the classic VB runtime and. change the value received to the parameter (that is, the value that was sent to the method as the argument). Remember, once the value is passed to the parameter, changing the value in the method does

Ngày đăng: 14/08/2014, 01:20

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan