1.4.9 Representing Parameters in Class Diagrams

Representing Parameters in Class Diagrams

The Wildcats need a method that accepts a jersey number. A class diagram shows the input contract for every call, not the number used by one call.

Read the Code Before Drawing

This complete Player definition has one public field and one public method with one integer parameter.

If the underlying C# feature is unfamiliar, use the lesson below while reading this example.

Source to translate
public class Player
{
    public int jerseyNumber;

    public void SetJerseyNumber(int number)
    {
        jerseyNumber = number;
    }
}
Rewrite the Meaning in Ordinary Words
The same design in ordinary words
Define a Player with an integer jerseyNumber field
Offer a public SetJerseyNumber operation
The operation accepts one integer input named number
It stores that input and returns no value

These words describe the class contract, not a sequence of runtime actions. Identify the types, stored members, operation inputs and results, and relationships before choosing compartments and connectors.

Map the Source to Class Boxes and Connectors
Build the Diagram in Violet
  1. Create a class diagram in Violet and add Player.
  2. In the attributes compartment enter +jerseyNumber : int. This is the member that retains state after a call.
  3. In the operations compartment enter +SetJerseyNumber(number : int) : void. Keep the parameter inside the parentheses and the return type afterward.
  4. Do not put number in the attributes compartment or replace it with 7. Those changes would confuse method input with stored state or a particular call.
  5. Save and read the operation aloud: a public call accepts one integer number and returns no value.
Compare the Completed Soccer Diagram

Compare this reference with the diagram you built in Violet. Layout may differ; the source meaning, labels, and relevant connections should agree.

Player has a public integer jersey field and a public SetJerseyNumber operation accepting parameter number of type int, returning void.
An integer input in an operation contract
  • New
  • Changed
  • Removed
Read the Contract One Part at a Time

The operation entry accepts different integers without changing the diagram. Calling it with 7 or 12 changes a runtime value, not this signature.

Verify the Drawing Against the Source
Predict and Check a Small Change