How to Create a Method, Call a Method and Return Values from a Method.

Part of the You Can Learn C# series.
By Ken Brown
Editor, YouCanLearnSeries.com
Updated:
September 5, 2004

This is part 1 of a three part article on Using Methods, calling a Method, and returning values from a method.

Using Methods and Calling a Method in C#
Passing Objects to a Method
Returning Values from a Method.

In this article we are going to discuss the programming tool known as a Method call. Back in the earlier days of programming the Method was known as a Procedure or if it returned a value known as a Function. With C# they are all called methods, whether a value is returned or not.

You will learn how to pass values to a Method, return values from a Method and what to do inside a Method. The concept of little sections of code that act as little black boxes is the foundation of object-oriented programming. The ability to pass a value into an object and have it return something of value has lead to programming as we know it today.

Let's start by creating our own little program that utilizes a simple method and then build to the ability to pass values in and return objects from the method. To begin create a solution and a project. For information on how to create a solution and project see our article titled Create a Project or use a solution you have already created.
  1. I created a new project titled YCLSProj2 within a solution titled MyFirstSolution.
  2. Then to give us some objects to work with drag and drop from the toolbox
    a button control, Label control and a TextBox control.
    that will be enough to get us started.
  3. Give the Button control an ID property of "btnActivate" and a Text property of "Activate"
    For the Label an ID property of lblAnswer with no Text property.
    For the TextBox an ID property of txtInput with no Text property.
  4. Right click somewhere on WebForm1.aspx and choose menuitem View Code.
  5. Within the class create a new method.
    private void ChangeLabel()
    {
        lblAnswer.Text = "Change the Label Method.";
    }
    When this method is called it will change the text of our label on the form to read "Change the Label Method."
  6. Now we have to actually call the method. Go to the Page_Load event where you see the wording "// Put user code to initialize the page here".
    Underneath that wording type in ChangeLabel();
  7. You can now Run your application by clicking on the Start button (Blue arrow).
    if we did this right you should see an image similar to what is below.

    Changing a label text property by calling the ChangeLabel()  Method
  8. Now let's step through the code and see what is occurring.
  9. While the application is running, Go To the code behind page within Visual Studio.
  10. Under the Page_Load event where you placed your ChangeLabel() add a breakpoint by clicking once on the left side frame. A red dot should show up on the frame
    and a red highlight should highlight the Text.
  11. Now hit the Refresh button on your Running application. It should stop where you placed your breakpoint
  12. Now you want to step into your code by clicking the Step Into button
    on the toolbar or if you are emulating C++ mode, choosing the F11 button.
    This action will take you right into the method named ChangeLabel and will stop on the next section of code to be run. (lblAnswer.Text = "Change the Label Method.";)
  13. To finish up click on the start button.
So at this point you have created a method called ChangeLabel and it has produced a change to your form when you ran the application. Now, when you code you don't have to bunch up all your code into one single event or method. Break up your code into relevant pieces and make them methods. Then when you call the methods something will occur and you can call these methods over and over so you only have to write this code once. This is the foundation of Object Oriented Programming and will lead you into the mind set of the professional programmer. Where everything is an object or a little black box with the ability to get information and / or return information.

In part two we continue on this journey, but I will show you how to return information or objects from a method.

Go To Page  1 / 2 / 3


Home| About Us | NewsLetters | Contact Us |

Copyright © 2004-2014 You Can Learn Series

You Can Learn Series Home Page You Can Learn Series
You Can Learn Series Home Page Home            About the creators of YouCanLearnSeriesAbout Us      Contact YouCanLearnSeriesContact Us      Site map of YouCanLearnSeriesSite Map      Privacy Policy of You Can Learn SeriesPrivacy Policy       Terms of Use of You Can Learn Series web siteTerms of Use       YouCanLearnSeries RSS feed for C# tips and tutorials     
C# and ASP.Net Programming TipsC# and ASP.Net      SQL Tutorial, tips and tricks for DBA'sSQL       Landscaping Tips for Home GardenersLandscaping       WeightLifting Tips for the Young and OldWeightLifting       Kennos BlogBlog       Good health TipsGoodHealth       Learn about Great Travel DestinationsTravel     Web Business TipsWeb Business            You Can Learn Series' Online StoreStore



You Can Learn C# and ASP.Net SeriesC# and ASP.Net
  Using Client Side Code with C# and ASP.NetClient Side Code
  Using Client Side State Management with C# and ASP.NetClient Side State Management
  Create a C# Class in Visual StudioCreate A Class
  Use A Class in C#Use A Class
  Create a Solution in Visual StudioCreate a Solution
  Create a Project in Visual StudioCreate a Project
  Create an RSS feed for your Web SiteCreate an RSS Feed
  Create an RSS feed for your Web SiteListItemCollection
  Set Events in ASP.NetSet Events ASP.Net
  Set Events in C#Set Events C#
  What is Session State and How to Use it.Session State
  Creating and Using the C# SortedList ObjectSortedList Object
  Stylesheet Tips for Visual Studio .Net and ASP.NetStyleSheet Tricks
  Using C# MethodsUsing Methods
  Pass Objects to MethodsPass Objects to Methods
  Pass Objects from MethodsPass Objects from Methods
  Using Client side State management like ViewState and Hidden fieldsView State
  LDAP, What is Lightweight Directory Access ProtocolWhat is LDAP
  Build an LDAP ReaderBuild an LDAP Reader
  Convert information from LDAP to a Web ServiceLDAP To Web Service
  Question of the dayQuestion of the Day

You Can Learn SQL SeriesSQL
You Can Learn Landscaping and Gardening SeriesLandscape Tips
You Can Learn WeightLifting SeriesWeightLift Tips
Kenno's BlogBlog
You Can Learn Good Health SeriesGood Health Tips
You Can Learn Good Series NewslettersNewsletters
Learn about Great Travel DestinationsTravel
You Can Learn Series Web Business TipsWeb Business Tips
Merchandise Available from You Can Learn SeriesMerchandise

Sara A Mason - Sci-Fi Author of Renegades




Americas Public Schools