Search for question
Question

CHAPTER 7 Using Methods il 0 (continued) then save and compile the program. An error message appears, as shown 6. Remove the keyword static from the DisplayCompany Logo () method, and the nonstatic DisplayCompany Logo () method. The error occurs because in Figure 7-8. The message indicates that an object reference is required for the Main () method is static and cannot call a nonstatic method without an object reference (that is, without using an object name and a dot before the method call). Remember that you will learn to create objects in Chapter 9, and then you can create the types of methods that do not use the keyword static. For now, retype the keyword static in the DisplayCompany Logo() method header, and compile and execute the program again. DemoLogo.cs(7,7): error CS0120: An object reference is required for the non-static field, method, or property 'DemoLogo.DisplayCompany Logo()' Figure 7-8 Error message when calling a nonstatic method from a static method 7. Remove the keyword private from the header of the DisplayCompany Logo() method. Save and compile the program, and then execute it. The execution is successful because the private keyword is optional. Replace the private keyword, and save the program. When you wri following item . The data t . A local ide For example a tax as 7 pe method cou static vo You can th parameter The para Display will be k priva { d } Figu Writing Methods That Require a Single Argument ome methods require additional information. If a method could not receive arguments, hen you would have to write an infinite number of methods to cover every possible tuation. For example, when you make a dental appointment, you do not need to employ different method for every date of the year at every possible time of day. Rather, you n supply the date and time as information to the method, and no matter what date d time you supply, the method is carried out correctly. If you design a method to mpute an employee's paycheck, it makes sense that you can write putePaycheck() and ethod named/n-6. ch method iable enerate a Writing Methods with No Parameters and No Return Value You Do It Calling a Method In this section, you write a program in which a Main() method calls another method that displays a company's logo. 1. Open a new program named DemoLogo, and enter the statement that allows the program to use System.Console methods without qualifying them. Then type the class header for the DemoLogo class and the class-opening curly brace. using static System.Console; class DemoLogo { then calls the DisplayCompany Logo () method. 2. Type the Main () method for the Demo Logo class. This method displays a line, static void Main() { } Write("Our company is "); DisplayCompany Logo(); 3. Add a method that displays a two-line logo for a company. private static void DisplayCompany Logo () { 275 WriteLine("See Sharp Optical"); WriteLine("We prize your eyes"); } 4. Add the closing curly brace for the class (}), and then save the file. 5. Compile and execute the program. The output should look like Figure 7-7. Our company is See Sharp Optical We prize your eyes Figure 7-7 Output of the DemoLogo program (continues)

Fig: 1

Fig: 2