1 / 8

Chapter 6 Methods Review

Chapter 6 Methods Review. Find the error in the following method: public void SayHello (); { MessageBox.Show ("Hello"); } Should not have semicolon on the header line Following is a message header line: public void ShowValue ( int intMyNum )

kort
Télécharger la présentation

Chapter 6 Methods Review

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. Chapter 6MethodsReview

  2. Find the error in the following method: public void SayHello();{MessageBox.Show("Hello"); } Should not have semicolon on the header line Following is a message header line:public void ShowValue (intintMyNum) Find the error in the following call to the method:intintNum = 8;ShowValue(intintNum); Call to ShowValue should not contain variable data type (int) Slide 2

  3. Find the error in the following method: public double TimesTwo(double dblNum){double dblResult = dblNum * 2; } Should have a return statement to return dblResult Find the error in the following method: public int Half(double dblNum){double dblResult = dblNum / 2.0; return dblResult; } Method header should have return type double, not int Slide 3

  4. Given the code below in a click event, what will be displayed? int intNumA = 5, intNumB = 10; MessageBox.Show("NumA is " + intNumA + " and NumB is " + intNumB ); ChangeUs(intNumA, intNumB); MessageBox.Show("now NumA is " + intNumA + " and NumB is " + intNumB); void ChangeUs(int intFirst, int intSecond) { intFirst = 20; intSecond = 30; } NumA is 5 and NumB is 10 now NumA is 5 and NumB is 10 Slide 4

  5. What must be changed in the code to allow the method to change the numbers? int intNumA = 5, intNumB = 10; MessageBox.Show("NumA is " + intNumA + " and NumB is " + intNumB ); ChangeUs(intNumA, intNumB); MessageBox.Show("now NumA is " + intNumA + " and NumB is " + intNumB); void ChangeUs(int intFirst, int intSecond) { intFirst = 20; intSecond = 30; } Change the call statement:ChangeUs(ref intNumA, ref intNumB); Change the method header:void ChangeUs(ref int intFirst, ref int intSecond) Slide 5

  6. Write an example of a call statement given the following method header: public void DoSomething(intintNum) DoSomething(25); Given the method definition below, write a statement that passes the value 4 to the method and assigns a return value to intResult?public int Cube(intintNum) { return intNum * intNum * intNum; } intResult = Cube(4); Slide 6

  7. A program contains the following method: public void Display ( int intX, double dblY, bool blnZ){ MessageBox.Show( “X is “ + intX + “ Y is “ + dblY + “ and Z is “ + blnZ);} Write a statement that calls this method and passes the following variables:bool blnCheck = false; int intAge = 25; double dblIncome = 50000.00; Display(intAge, dblIncome, blnCheck); Slide 7

  8. Write a method named TimesTen. The method should accept a double argument and return a double value that is 10 times the value of the argument. public double TimesTen(double dblNum) { return dblNum * 10; } Slide 8

More Related