Wrox Programmer Forums
|
BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3
This is the forum to discuss the Wrox book Beginning C# 3.0 : An Introduction to Object Oriented Programming by Jack Purdum; ISBN: 9780470261293
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 18th, 2010, 11:40 PM
Authorized User
 
Join Date: Feb 2010
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default overloaded methods

hi

i got a bit lost on pages 132... can you please give me another example other than the date/time example on how i could use overloaded methods?
 
Old February 19th, 2010, 01:46 AM
Friend of Wrox
 
Join Date: Sep 2008
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default Overloading methods

You probably don't need an example, but rather a better understanding of what overloading is and what it brings to the table. The class constructor is a good example. Suppose I create a class that has a person's name and their address. Let's call the class clsBirthdayCards and it has the following members:

string name;
string addr;
string city;
string state;
int zip;

I wrote such an application once and most of the people that would be in the database would be from Indiana. Normally, I would create an instance of the class with:

clsBirthdayCards myCards = new clsBirthdayCards();

Notice that clsBirthdayCards() is the default constructor for this class. When called this way, the members of the class are initialized with default values (null for string, zero for the int). Visual Studio writes the necessary code for you...and doesn't even show it to you. However, because I knew that most of the people in the list would be from Indiana, I overloaded the constructor and wrote one that looked like:

public class clsBirthdayCards()
{

string name;
string addr;
string city;
string state;
int zip;

public clsBirthdayCards(string whichState)
{
state = whichState;
}

// Rest of class code
}

Now I have two class constructors, one that I can call with:

clsBirthdayCards myCards = new clsBirthdayCards();

and one I can call with:

clsBirthdayCards myCards = new clsBirthdayCards("Indiana");

Note that because the methods names are the same, the constructor is "overloaded" (i.e., there's more than one flavor of the constructor). The compiler knows which one to call based on whether there is an argument or not. The advantage is that I now have a class member named state that has a default value of "Indiana" assigned into it.

So when do you overload a method? For constructors, if you're not happy with the default values that would be assigned to the default constructor, overload it and pass in the values you want. For other methods, overloading is used when you want to use the same method name, but you need the functionality to be different in certain cases. Overloading requires that the method signatures be different.

Read the contructor overloading section, starting on p.235 over and over until you fully understand it.
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
 
Old February 19th, 2010, 02:33 AM
Authorized User
 
Join Date: Feb 2010
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Read the contructor overloading section, starting on p.235 over and over until you fully understand it.
hi dr purdum

thank you so much ... ok i will read it over and over again once i get to it ... im on page 227 now ... :)





Similar Threads
Thread Thread Starter Forum Replies Last Post
METHODS magagulad Java GUI 3 May 15th, 2007 12:53 PM
What's the difference between these two methods? aaaa0441 Beginning PHP 2 January 18th, 2007 09:12 AM
Treeview Methods goldwinger C# 2005 0 December 29th, 2005 10:00 AM
How to call C# COM overloaded functions from ASP? khaledriyal C# 0 October 11th, 2005 03:13 AM
C# Overloaded Method argument problem Scott Rider C# 1 January 3rd, 2005 06:39 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.