Passing Arguments and Returning Values in C# WinForms

Make your WinForms methods powerful and flexible

Once you start creating your own methods, the next step is making them dynamic β€” by passing in arguments (parameters) and getting back return values.

This makes your code reusable and lets you separate logic from interface code β€” a core principle of clean app development.


🧰 Why Use Parameters and Return Values?

  • πŸ”„ Arguments let methods work on different data
  • πŸ”™ Return values let methods give results back
  • βœ… Keeps your UI event handlers short and smart

🏷 What’s an Argument?

An argument (or parameter) is a value you pass into a method so it can work on it.

private void ShowGreeting(string name)
{
    labelResult.Text = $"Hello, {name}!";
}

Call it from a button click:

string user = txtName.Text;
ShowGreeting(user);

βœ… Now ShowGreeting() works for any name you pass in.


🧱 Multiple Parameters

private void ShowProfile(string name, int age)
{
    labelResult.Text = $"Name: {name}, Age: {age}";
}

Then:

ShowProfile("Emma", 25);

Or dynamically:

string user = txtName.Text;
int age = int.Parse(txtAge.Text);
ShowProfile(user, age);

πŸ”„ What’s a Return Value?

You can create a method that calculates something and returns it.

private int Add(int a, int b)
{
    return a + b;
}

Then:

int result = Add(5, 3);
labelAnswer.Text = $"Result: {result}";

βœ… The method does the work, then gives back the result.


πŸ’‘ Example: Total Calculator

private double CalculateTotal(double price, int quantity)
{
    return price * quantity;
}

Use it like:

double price = double.Parse(txtPrice.Text);
int qty = int.Parse(txtQuantity.Text);

double total = CalculateTotal(price, qty);
lblTotal.Text = $"Β£{total}";

⚠️ Return Types Must Match

This will cause an error:

private string AddNumbers(int a, int b)
{
    return a + b;  // ❌ Type mismatch β€” returning int, not string
}

Fix it with conversion:

private string AddNumbers(int a, int b)
{
    return (a + b).ToString();
}

πŸ”„ You Can Return bool, Too

private bool IsAdult(int age)
{
    return age >= 18;
}

Then:

int age = int.Parse(txtAge.Text);
if (IsAdult(age))
    label1.Text = "You are an adult.";
else
    label1.Text = "You are under 18.";

πŸ§ͺ Quick Challenge

🧩 Create a form with:

  • 2 TextBox controls: Number 1, Number 2
  • 1 Button: Calculate
  • 1 Label: Show result

Write a method:

private int AddNumbers(int a, int b)

Call it from the Button.Click event and show the result in the label.


πŸ“š Summary

ConceptDescription
ArgumentValue passed into a method
Return ValueResult sent back by a method
voidUse when method doesn’t return anything
Return typesCan be int, string, bool, double, etc.

βœ… Best Practices

  • βœ… Give clear, specific method names: CalculateTotal(), IsValidName()
  • βœ… Keep your methods short and focused on one task
  • βœ… Validate inputs before parsing (TryParse)
  • βœ… Use return values instead of repeating logic inside events

πŸŽ“ Want to Go Further?

  • Use ref and out to return multiple values
  • Combine logic and validation into returnable helper methods
  • Create your own utility classes to organize methods
  • Explore method overloading (same method name, different parameters)

πŸ’¬ Need help turning your UI logic into clean, reusable methods?
Send a screenshot or snippet β€” I’ll help you modularize and simplify it or join our WinForms C# Course to learn more.