Summary This chapter introduced you to two of OOP's most powerful features: inheritance and polymorphism. Knowing how to implement these features is fundamental to becoming a successful object-oriented programmer, regardless of the language you use. In the next chapter, you will take a closer look at how the objects in your applications collaborate. The topics covered include how objects pass messages to one another, how events drive your programs, how data is shared among instances of a class, and how exceptions are handled.
Using the DataReader Object to Retrieve Records To use the DataReader object to retrieve records, follow these steps: 1. Open the Author class code in the code editor. Add a public method to the class definition called GetAuthorList that returns an generic List of strings: public List<string> GetAuthorList() { } Add the following code, which executes a SQL Select statement to retrieve the authors’ last names. A DataReader object then loops through the records and creates a list of names that gets returned to the client. SqlCommand authorsCommand = new SqlCommand(); SqlDataReader authorDataReader; List<string> nameList = new List<string>(); try { authorsCommand.Connection = _pubConnection; authorsCommand.CommandText = "Select au_lname from authors"; _pubConnection.Open(); authorDataReader = authorsCommand.ExecuteReader(); while (authorDataReader.Read() == true) { nameList.Add(authorDataReader.GetString(0)); } return nameList; } catch (SqlException ex) { throw ex; } finally { if (_pubConnection != null) { _pubConnection.Close(); } } Change the code in the Main Method of the Program class to show the list of names in the console window. static void Main(string[] args) { try { Author author = new Author(); foreach (string name in author.GetAuthorList()) { Console.WriteLine(name); } Console.ReadLine(); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.ReadLine(); } } Select Debug > Start to run the project. The Console window should launch with the names of the authors displayed. After viewing the output, stop the debugger.
The Show method is overloaded so that you can optionally show a MessageBox icon, show a title, change the buttons displayed, and set the default button. The only required setting is the text message to be displayed on the form. Figure 11-10 shows the MessageBox displayed by the previous code. Figure 11-10. A basic MessageBox The following code calls the Show method using some of the other parameters. Figure 11-11 shows the resulting MessageBox that gets displayed. For more information on the various parameters and settings available, look up the MessageBox class in the Visual Studio help file. MessageBox.Show("Are you sure you want to quit?", "Closing Application",MessageBoxButton.OKCancel, MessageBoxImage.Question); Figure 11-11. A more complex Messagebox You will often use a MessageBox to query for a user response to a question. The user responds by clicking a button. The result is passed back as the return value of the MessageBox.Show method in the form of a MessageBoxResult enumeration. The following code captures the dialog box result entered by a user and closes the window (or not) depending on the result: MessageBoxResult result = MessageBox.Show("Are you sure you want to quit?", "Closing Application",MessageBoxButton.OKCancel, MessageBoxImage.Question); if (result == MessageBoxResult.OK) { this.Close(); }
} [DataMember] public double Longitude { get { return _longitude; } set { _longitude = value; } } } By marking the classes with the [DataContract] and [DataMember] attributes, an XSD file is created describing the complex types. Clients use this file to determine what to supply the service and what to expect as a return type. Figure 13-7 shows the portion of the XSD file created for the Location type returned by the service. ocs:element min0ccurs="0" name-'Latitude" type="xs:double" {> ocs:element minGccurs="0" name="Longitude" type="xs:double" /> ■ocsielement name-'Location" nillable-'true" type-'tns:Location" /> Figure 13-7. XSD file defining the Location type, as displayed in a browser Let's put what you've learned so far to work by building a simple service that supplies a list of stores from the Pubs database. The service will then be consumed in a Silverlight client to display a list of stores. ACTIVITY 13-1. CREATING AND CONSUMING A WCF SERVICE In this activity, you will become familiar with the following: • Creating a WCF Service. Consuming a WCF Service in a Silverlight client. Creating a WCF Service To create the WCF Service, follow these steps: 1. Start Visual Studio. Select File > New > Project. Choose a Silverlight Application under the C# Projects folder. Rename the project to Act13_1 and click the OK button. The next screen asks if you want to host the Silverlight application in a new web site. It also asks you what version of Silverlight you want to use. Accept the defaults and click OK. Right-click on the Act13_1 .Web project node in the Solution Explorer window and select Add > New Item. In the Add New Item window, click on the Web node in the Installed Templates section. Select the WCF Service template, rename it to PubsService, and click the Add button (see Figure 13-8).