Teaching%20Session%203 - E n g in e e r in g 1 D 0 4...

Info icon This preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Engineering 1D04 Teaching Session 3 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 1 void doIt() { textBox1.Text = "Hello Hamilton!!"; } Introduction to Methods In Lab 2 you were introduced to creating simple methods. Let’s recap
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 2 ClickMe Button handler doIt() MouseOverMe Button handler Text in Textbox ClickMe Button Pushed MouseOverMe Button Entered void doIt() { textBox1.Text = "Hello Hamilton!!"; } Introduction to Methods © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 3 doIt has no inputs (no parameters) and no outputs (a void return type). void doIt() Method Signature Return Type Parameters Introduction to Methods
Image of page 2
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 4 To make doIt() more useful we can add a parameter (an input). void doIt2(string string4Textbox) { textBox1.Text = string4Textbox; } Introduction to Methods © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 5 doIt2("Hello Hamilton!!"); Introduction to Methods When doIt2 is called, we now need to pass it a parameter.
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 6 doIt2("Hello Hamilton!!"); void doIt2(string string4Textbox) { textBox1.Text = string4Textbox; } "Hello Hamilton!!" Introduction to Methods A copy of the string "Hello Hamilton!!" is then stored within the variable string4Textbox. © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 7 doIt2 could also be called with a string variable as a parameter. string myStringVariable = "Hello"; doIt2(myStringVariable); void doIt2(string string4Textbox) { textBox1.Text = string4Textbox; } "Hello" Introduction to Methods
Image of page 4
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 8 A copy of the variable's data is passed as a parameter. The copy is then no longer associated with myStringVariable. string myStringVariable = "Hello"; doIt2(myStringVariable); void doIt2(string string4Textbox) { textBox1.Text = string4Textbox; } "Hello" Introduction to Methods © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 9 string sum(int x1, int x2) { return Convert.ToString(x1 + x2); } Method Parameters Let’s create a method to add two numbers together and return a string containing the result. We need 2 inputs and 1 output from the method. output inputs
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 10 Multiple parameters can be given by separating them with commas. Replacing the void with string gives the method the return type string. The keyword return specifies the output value and ends the method call. string sum(int x1, int x2) { return Convert.ToString(x1 + x2); } Method Parameters What does that mean? © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 11 Methods The keyword return specifies the output value and ends the method call. Ending the call to the method means that there is no more processing done by that method and control returns to the action immediately after the call.
Image of page 6
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 12 private void button1_Click(object sender, EventArgs e) { textBox1.Text = sum(4,3); textBox1.Visible = true; } string sum (int x1, int x2) { return Convert.ToString(x1 + x2);
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern