Visual studio 2010 part 11 pdf

9 304 0
Visual studio 2010 part 11 pdf

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

Thông tin tài liệu

This page intentionally left blank 89 Chapter 4 Learning Just Enough C# and VB.NET: Intermediate Syntax 90 Microsoft Visual Studio 2010: A Beginner’s Guide Key Skills & Concepts ● Use Delegates and Events ● Implement Interfaces ● Code with Arrays and Generics I n previous chapters, you learned basic syntax and how to create your own types. This chapter rounds out the bare essentials of what you need to know with delegates and events, interfaces, and a quick introduction to arrays and generics. This material doesn’ t attempt to be too advanced, but gives you enough information to understand the language concepts involved. You’ll see all of these language features being used throughout the book, and it’s good to have some background on what they mean. Let’s start off with delegates and events. Understanding Delegates and Events Sometimes you need to write flexible code that performs general operations. For example, when the designers of the .NET Framework created user interfaces, they added reusable controls, such as buttons, list boxes, and grids. When writing these controls, the framework designers didn’t know how we would use them. For example, how would anyone know what we wanted our code to do when a user clicks a button on the user interface? So, these controls have interaction points built in so that they can communicate with your program; these interaction points are called events. These events fire whenever a user performs an action such as a button click or a list box selection. We write code to hook up these events to some other code in our program that we want to run when that event happens, such as when the user clicks a button, and this is what delegates are used for. An event defines the type of notifications that a object can provide, and a delegate allows us to connect the event to the code we want to run. This section will show you the mechanics of how delegates and events work, but you should understand that the mechanics may seem somewhat abstract at first. Delegates and events are most often used when you’re working with .NET Framework technologies that use them, such as Windows Presentation Foundation (WPF), Silverlight, and ASP.NET. What you’ll want to do is get a feel for the mechanics right now and then refer back to this discussion when you encounter delegates and events in later chapters. Chapter 4: Learning Just Enough C# and VB.NET: Intermediate Syntax 91 The next section will add more logic to the set accessor in CurrentBalance in the next listing and raise an event for the calling code. Events An event is a type of class member that allows your class or class instance to notify any other code about things that happen within that class. To help you understand the use of events, this section will associate an event with the accountBalance of an account. Listing 4-1 is a modified version of Listing 3-8 from Chapter 3. It additionally has an event and logic that raises the event. To see how an event can be useful, consider a program that uses a class that manages accounts. There could be different types of accounts, such as checking or savings. If a customer performs an overdraft, the consequences probably vary by what type of account is being used. However, all you want is a generalized account class that can be used by any bank account type and doesn’t know what the overdraft rules are, which makes the class more reusable in different scenarios. Therefore, you can give the account class an event that will fire off a notification whenever an overdraft occurs. Then, within your specialized checking account class instance, for example, you can register something called an event handler so that the instance of the class knows each time the overdraft event occurs via the handler. In Listing 4-1, the CurrentBalance property is modified to raise (or fire off) an OverDraft event whenever the assigned value is less than 0. The Main method hooks up another method that will run whenever that event occurs. I’ll explain the event first and then follow up with a discussion of how to hook up a method that listens for when the event is raised and receives the message sent by the event. Listing 4-1 Event demo C#: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace FirstProgram { class Program { private decimal accountBalance = 100m; static void Main(string[] args) 92 Microsoft Visual Studio 2010: A Beginner’s Guide { Program account = new Program(); account.OverDraft += new EventHandler(account_OverDraft); account.CurrentBalance = -1; Console.ReadKey(); } public decimal CurrentBalance { get { return accountBalance; } set { if (value < 0) { if (OverDraft != null) { OverDraft(this, EventArgs.Empty); } } accountBalance = value; } } static void account_OverDraft(object sender, EventArgs e) { Console.WriteLine("Overdraft Occurred"); } public event EventHandler OverDraft; } } VB: Module Module1 Private Dim accountBalance As Decimal = 100 Sub Main() AddHandler OverDraft, AddressOf AccountOverdraft CurrentBalance = -1 Chapter 4: Learning Just Enough C# and VB.NET: Intermediate Syntax 93 Console.ReadKey() End Sub Public Event OverDraft As EventHandler Public Sub AccountOverdraft (ByVal sender As Object, ByVal e As EventArgs) Console.WriteLine("Overdraft Occurred") End Sub End Module Listing 4-1 has an event named OverDraft. The OverDraft event is public and is declared with the event keyword. The EventHandler is a delegate, which we’ll discuss soon, but it basically allows you to define the type of method that can be called by the event. It defines the communication contract that must be adhered to by any code that wishes to listen for the event to fire. Look at the set accessor of the CurrentBalance property, inside of the if statement where it determines if value is less than 0. The C# example has another if statement to see if the OverDraft event is equal to null. In C# when an event is equal to null, it means that nothing has subscribed to be notified by the event—in essence, no other code is listening. However, when the C# event is not null, then this indicates that some code somewhere has hooked up a method to be called when the event fires. That method is said to be listening for the event. So, assuming that the caller has hooked up a method, the OverDraft event is fired. This check for null is important. If nothing is listening for the event (and our code knows this to be the case when the event is null), and we raise or fire the event by calling OverDraft(this, EventArgs.Empty), an error (null reference exception) would occur at runtime whenever a value is set into the CurrentBalance property. The arguments to the C# event mean that the current object (which is the Program class instance), this, and an empty EventArgs will be passed as the event message to any other methods that were hooked up to this event. It is interesting to note that many methods can be hooked up to your event (or none at all), and each will be notified in turn when your event fires. You should start to see that events really are a form of almost spontaneous communication within your program. In VB, you don’t need to check for Nothing (equivalent to C# null). The preceding discussion talked about a method that is hooked up to the event and executes (receives a message) whenever the event fires. The next section explains how to use a delegate to specify what this method is. 94 Microsoft Visual Studio 2010: A Beginner’s Guide Delegates Delegates let you hook up methods as the receiver to specific events. The delegate specifies the allowable signature, the number of arguments, and their types, of a method that is allowed to be hooked up to the event as a listener or handler. The EventHandler delegate type for the OverDraft event specifies what the signature of a method should be, as follows: C#: public event EventHandler OverDraft; VB: Public Event OverDraft As EventHandler This EventHandler is a class that belongs to the .NET Framework class library, and it, by definition, specifies that any methods hooked up to the OverDraft event must define two parameters: an object of any type and an EventArgs class. EventHandler also specifies that the method does not return a value explicitly. The following method, account_ OverDraft (AccountOverdraft in VB), matches the predefined EventHandler signature: C#: static void account_OverDraft(object sender, EventArgs e) { Console.WriteLine("Overdraft Occurred"); } VB : Public Sub AccountOverdraft(ByVal sender As Object, ByVal e As EventArgs) Console.WriteLine("Overdraft Occurred") End Sub Notice that the C# account_OverDraft (AccountOverdraft in VB) doesn’t return a value and has two parameters that are type object and EventArgs, respectively. The account_OverDraft (AccountOverdraft in VB) method is hooked up to the OverDraft event in the Main method in Listing 4-1, repeated as follows for your convenience: C#: account.OverDraft += new EventHandler(account_OverDraft); account.CurrentBalance = -1; VB: AddHandler OverDraft, AddressOf AccountOverdraft CurrentBalance = -1 Chapter 4: Learning Just Enough C# and VB.NET: Intermediate Syntax 95 In the C# example, the += syntax is for assigning a delegate to an event (using a bit of programmer slang, this syntax is commonly said to “wire up an event”). The VB example uses AddHandler and AddressOf to assign the AccountOverDraft method to the OverDraft event. In the C# example, the delegate is a new instance of EventHandler and the event is OverDraft. If you remember, the delegate type of OverDraft is Eventhandler, which defines the precise message contract. The next piece of the puzzle is the method to be notified when the event happens. This method is the parameter given to the new EventHandler delegate instance. You saw earlier where the account_OverDraft (AccountOverDraft in VB) method had the signature specified by the EventHandler class, making it possible for our method to be specified as the new EventHandler parameter. With that one line of code (the one with the += statement), account_OverDraft (AccountOverdraft in VB) is now hooked up to the OverDraft event. This means that when the value of CurrentBalance is set to less than zero via the set accessor of CurrentBalance, the OverDraft event gets fired because the OverDraft(this, EventArgs.Empty) is called, which then invokes the account_OverDraft (AccountOverdraft in VB) method (the method we wired up to the event), which in turn executes its code. One more note about events: you’ll see them used extensively in graphical user interface (GUI) code. Think about the GUI code that has reusable components, like buttons and list boxes. Every time the user clicks a button or selects an item in the list box, you want code to execute and do something, like perhaps save the user’ s data somewhere. You do this through events: a Click event for the button and a SelectedItemChanged for the list box. This is the standard way that you program GUIs; you have an event and you define a method to hook up to that event so that your running program can do some work in reaction to the user. Event, Delegate, and Handler Code Completion While there isn’t a snippet, per se, to create an event or delegate, in C# there is Intellisense Code Completion support for hooking a delegate up to an event, which also generates the handler method. The process takes two steps: delegate and handler creation. To get started, type the reference to the event’s containing instance, the event name, and +=. As soon as you type the = sign, you’ll see a tooltip like the one in Figure 4-1. Figure 4-1 Code completion for delegate assignment 96 Microsoft Visual Studio 2010: A Beginner’s Guide As you can see, the Editor pops up a tooltip instructing you to type TAB to create a new delegate instance. Type TAB and Code Completion will pop up another tooltip for creating the handler method, as shown in Figure 4-2. In Figure 4-2, you can see that Code Completion is suggesting a method name for you. You have a choice of pressing TAB or changing the method name and then pressing TAB. Either way, you have a fast way to hook up a handler method to an event via the event’s delegate type. Just as a delegate provides an interface to a method that is a contract basically to describe how to communicate, you can also define interfaces to classes to communicate with them in a specified way, and these are intuitively named . . . interfaces. Implementing Interfaces Another language feature that gives you flexibility is interfaces. An interface can be useful if you want to have a group of classes that can be interchanged at any time, yet you need to write the same operations for each of these classes. Essentially, you want to write the code that uses the class only one time, but still switch what the actual class is. That’s where interfaces come in. The interface creates a contract that each of the interchangeable classes must adhere to. So, if the interface says that all classes that implement the interface have method A and property B, then every class that implements the interface must have method A and property B; the compiler enforces this like a contract that cannot be broken. The following sections show you how to write an interface and then build a couple of classes that implement that interface. Finally, you’ll see how to write code against the interface. One important fact to remember about interfaces is that they don’t have any code other than definitions of members. This definition of members is the contract of the interface. You are the one who must to write a class that contains the members of the interface, and you must write the code that provides an implementation of the interface members. A common point of confusion is that an interface does not have any executable code, but the classes that implement the interfaces do. The following sections show you how to create an interface, how to create a class that has code (that you’ve written) to implement the interface contract, and how to write code that operates on the classes that implement (guarantee the contract of) the interface. Figure 4-2 Code completion for handler method creation . blank 89 Chapter 4 Learning Just Enough C# and VB.NET: Intermediate Syntax 90 Microsoft Visual Studio 2010: A Beginner’s Guide Key Skills & Concepts ● Use Delegates and Events ● Implement. private decimal accountBalance = 100m; static void Main(string[] args) 92 Microsoft Visual Studio 2010: A Beginner’s Guide { Program account = new Program(); account.OverDraft += new. next section explains how to use a delegate to specify what this method is. 94 Microsoft Visual Studio 2010: A Beginner’s Guide Delegates Delegates let you hook up methods as the receiver to specific

Ngày đăng: 04/07/2014, 03: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