Docsity

Understanding Methods, Menus, and Dialog Boxes in Programming, Slides of C programming

Partial preview of the text

Download Understanding Methods, Menus, and Dialog Boxes in Programming and more Slides C programming in PDF only on Docsity!

Methods, Menus, and Dialog Boxes

05_methods_menus.ppt

Overview of Topics

  • Top-Down Design
  • Built-in Methods
  • Black Box Analogy
  • Programmer-Defined Methods
  • Method’s Signature & Overloading
  • Pass-by-value Arguments
  • Menu Designer
  • Context Menus (right-click)
  • Common Dialog Boxes

Top-Down Design - Analogy

  • A design method where the major task to

be accomplished is divided into subtasks.

  • Major Task: clean house
  • Subtasks: dust, vacuum, sweep, mop
  • Programming example.
  • Major Task: CS5 Sales Calculator
  • Subtasks: input qty, price calculate sales tax, shipping, subtotal, etc display total bill

Subtasks

  • Each subtask should perform a single well-defined task.
  • Each subtask may produce some result.
  • Treat them as small programs.

Input -> Process -> Output

  • These subtasks can be used at different times in

one program or placed in a library and used by many different programs.

  • The int.Parse method is a complicated task that

converts strings to numbers, and is used by many different programs.

Programmer-Defined Methods

  • Before coding some programmer-defined

methods, let’s look at some built-in methods

a little closer.

  • It will help understand what we are trying to

develop if we look at how methods are used.

Built-in Methods

  • They are like small programs.
  • They have their own input – process – output.
  • They can have one or more inputs.
  • But methods can only return one output.
  • As programmers, we don’t get to see the code

(process) of built-in methods.

  • The process is like a black box…

Convert Text to Integer – int.Parse

  • Convert Text to Integer method is named int.Parse.
  • The documentation for int.Parse states the value that will be returned, method name, and the type and number of arguments. intNumber = int.Parse(String) returnedValue = MethodName (arguments)
  • Int.Parse takes one argument that is a String.
  • The method is called using the following syntax:

If txtQuantity.Text contains “3” then the function call:

intNumber = int.Parse(txtQuantity.Text)

would return a value of 3 into intNumber.

Method Call

  • We issue a method call when we use it.
  • Processing control is passed to the method

until it completes its task and returns control

back to the calling method.

  • Most methods return a value back,

but only one value can be returned

through the return statement.

Method’s Signature & Overloading

  • Method overloading occurs when more than one method with the same name is defined, but differ in the parameter list.

MessageBox.Show(TextMessage); MessageBox.Show(TextMessage, TitleBar); MessageBox.Show(TextMessage, TitleBar, ButtonType); MessageBox.Show(TextMessage, TitleBar, ButtonsType, Icon);

  • Each method’s signature is unique and consists of:
    • Method name
    • The number, data types, and order of its parameters
    • Parameters can also be modified with the keywords ref and out (These keywords are covered in next powerpoint presentation)
    • The signature does NOT include the data type of the returned value and the names used to identify the parameters.

Small Programs

  • Think of each method as a small program with

input, process, and output steps.

Output < Process < Input

returnedValue = MethodName (arguments)

intNumber = int.Parse(String)

Function Definition - Syntax

  • Syntax: private dataType MethodName(arguments) { statement(s) return expression; }
  • dataType is where we specify what type of value is being returned, string or numeric.
  • Use the return statement to return the value.
  • Variables that will be used to store the values sent into the method are listed in parentheses as arguments.

Method Definition - Example

private decimal calcExtendedPrice(int intQty, decimal decPrice) { decimal decExtendedPrice; decExtendedPrice = intQty * decPrice; return decExtendedPrice; }

  • decimal is the data type of the value being returned.
  • calcExtendedPrice is the Method Name.
  • intQty and decPrice are the arguments passed to method.
  • The signature of this method is calcExtendedPrice(int, decimal)

Multiple Returns

  • A method may be defined with multiple Return statements.
  • The first Return statement executed returns the value and returns processing control back to the calling procedure.

private decimal calcExtendedPrice(int intQty, decimal decPrice { if (intQty < 26) return intQty * decPrice; else return intQty * (decPrice * .95); //5% discount }

Single Return Preferred

  • Although methods can be coded using multiple Return statements, one Return statement is preferred.
  • Having multiple Returns in complex procedure can be difficult to maintain and debug.

private decimal calcExtendedPrice(int intQty, decimal decPrice ) { decimal decExtendedPrice; if (intQty < 26) decExtendedPrice = intQty * decPrice; else decExtendedPrice = intQty * (decPrice * .95); // 5% discount

return decExtendedPrice; }