Part Two, Send objects to the Method and Process those Objects.

Part of the You Can Learn C# series.
By Ken Brown
September 5, 2004

This is part 2 in a three part article on Using Methods, calling a Method, passing values to the 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 part two of this article we are looking at returning values from a Method call. From page one we mentioned this is the old concept known as a Function. I will show you simple returned values such as strings and boolean values and then how to maximize those return values in your method calls. You will also learn how to pass values to the method.
  1. Let's begin by learning how to pass a value to the Method.
  2. Go back to the code behind in Visual Studio.
  3. In the Page_Load Event type in ChangeLabel("Hello Chicago"); where you originally had only the words ChangeLabel(); What we are trying to do is to pass in the words "Hello Chicago" to the ChangeLabel Method.
  4. Now we have to change the Method to receive values.
    Go to the code where we defined the ChangeLabel method.
    Looks like this: private void ChangeLabel()
    We are going to change it to accept a string object.
    After the Parenthesis in ChangeLabel add (string strLabel)
  5. The Method definition now looks like private void ChangeLabel(string strLabel).
    The Method is expecting you to pass in a string object, before it will do anything for you.
  6. Change the next line in the code to use the value passed into the method
    lblAnswer.Text = strLabel;
  7. Run the application and you should see "Hello Chicago" in the label.

Sending values from other controls to the Method.

The next step is to advance the Method call one minor step where we send a value that we type into the textbox on the form. Now just typing in the words you want to show up on the label isn't going to be enough. You will have to activate an event. We will place the event on the Activate button control.
  1. Go back to design view of the form.
    Double Click the Activate Button control which takes you to the EventHandler for the button control.
  2. We will call the ChangeLabel method, but we will pass in the value in the TextBox control. To capture the value from the textbox control use txtInput.Text.
  3. We then change our Method call to read: ChangeLabel(txtInput.Text.ToString());
  4. Run the application.
  5. Type in "Hello and your city name" into the textbox.

    ScreenShot showing screen before calling the Method with passing in the values from the textbox control.
  6. Then click on the Activate button.
    Whatever you type in the textbox will show up in the label.

    Changing the value in the label with the value in the textbox by passing this value to the ChangeLabel Method

    If you are having problems understanding the flow, set a breakpoint in the Activate Event and step through the code.

What Kind of Objects can be Passed to a Method?

What can you pass to a Method? Good question and the answer is just about any object supported by C#. You can send strings, DataSets, DataGrids, structs, collections, arrays and Numbers. You must remember you are passing by value and the scope of the is the same as the scope of the Method. Meaning when the method is finished, any calculations or changes to passed values end.

For example, you send in an int with a value of 15. You multiply that value by three and you now have an int value of 45. Then the method ends, and your value of 45 ends with it, unless you want to return the calculated number back to the object that called the Method. For more information on returning objects back from a method go on to Part 3, Returning Values 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

Bill Welter - The Prepared Mind of a Leader

Americas Public Schools