473,467 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

what is the use of over loaded functions.

Instead of using same name i can use different name,
is there any specific purpous is there to define the polymorphic
overloaded functions in c#

Jul 30 '07 #1
20 1343
<ve********************@gmail.comwrote in message
news:11**********************@i38g2000prf.googlegr oups.com...
Instead of using same name i can use different name,
True, but then it's not the same function...:-)
is there any specific purpous is there to define the polymorphic
overloaded functions in c#
It allows you to define different "versions" of the same function with
different signatures - other languages did something similar with optional
parameters, but C# doesn't, thank God!

http://www.csharpfriends.com/Article...?articleID=105
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 30 '07 #2
On Jul 30, 12:53 pm, veebhudhi.chandramo...@gmail.com wrote:
Instead of using same name i can use different name,
is there any specific purpous is there to define the polymorphic
overloaded functions in c#
For constructors, you can't change the name to start with.
For everything else - it's just more convenient not to have to create
lots of different method names.

Look at the overloads for Console.WriteLine, for example - would you
really want that many different method names?

Jon

Jul 30 '07 #3
One use of overloaded is to provide default parameter values.

Consider a function that takes three parameters with one parameter being
the same for 90% of the calls.
You could overload that function with one that takes two parameters and
delegate the call to your original function providing the third value
yourself.

My 2c.

ve********************@gmail.com wrote:
Instead of using same name i can use different name,
is there any specific purpous is there to define the polymorphic
overloaded functions in c#
--
Hitesh Ashar
hitesh (at) asharism (dot) com
Jul 30 '07 #4
Am Mon, 30 Jul 2007 13:02:26 +0100 schrieb Mark Rae [MVP]:
>
It allows you to define different "versions" of the same function with
different signatures - other languages did something similar with optional
parameters, but C# doesn't, thank God!
Optional parameters:
Why do you prefer

void f ( int data )
void f ( int data, bool withLogging )
over

void f( int data, bool withLogging = false )

???
Jul 30 '07 #5
"Paul Werkowitz" <ne********@primaprogramm.dewrote in message
news:1t*******************************@40tude.net. ..
void f( int data, bool withLogging = false )
That isn't an optional parameter - it's a parameter with a default value...

Not the same thing at all!
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 30 '07 #6
On Jul 30, 2:19 pm, "Mark Rae [MVP]" <m...@markNOSPAMrae.netwrote:
void f( int data, bool withLogging = false )

That isn't an optional parameter - it's a parameter with a default value...

Not the same thing at all!
I certainly see the two as being equivalent. There's no point in
having a default value unless the parameter is optional, and if it's
optional then surely it needs a default value.

What's the practical difference between the two in your view?

Jon

Jul 30 '07 #7
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@r34g2000hsd.googlegr oups.com...
On Jul 30, 2:19 pm, "Mark Rae [MVP]" <m...@markNOSPAMrae.netwrote:
void f( int data, bool withLogging = false )

That isn't an optional parameter - it's a parameter with a default
value...

Not the same thing at all!

I certainly see the two as being equivalent. There's no point in
having a default value unless the parameter is optional, and if it's
optional then surely it needs a default value.

What's the practical difference between the two in your view?
There's no practical difference, but an optional parameter (certainly in the
VB utilisation) doesn't actually have to exist...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 30 '07 #8
On Jul 30, 3:42 pm, "Mark Rae [MVP]" <m...@markNOSPAMrae.netwrote:
What's the practical difference between the two in your view?

There's no practical difference, but an optional parameter (certainly in the
VB utilisation) doesn't actually have to exist...
What exactly does that mean? What does a parameter which doesn't exist
look like?

Jon

Jul 30 '07 #9
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@57g2000hsv.googlegro ups.com...
On Jul 30, 3:42 pm, "Mark Rae [MVP]" <m...@markNOSPAMrae.netwrote:
>>What's the practical difference between the two in your view?

There's no practical difference, but an optional parameter (certainly in
the
VB utilisation) doesn't actually have to exist...

What exactly does that mean? What does a parameter which doesn't exist
look like?
Private Sub Draw(Optional X As Variant, Optional Y As Variant)
If IsMissing(X) Then X = 720
If IsMissing(Y) Then Y = 2880
Cls
Circle (X, Y), 700
End Sub

Draw()
Draw(600)
Draw(600, 1500)
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 30 '07 #10
On Jul 30, 4:22 pm, "Mark Rae [MVP]" <m...@markNOSPAMrae.netwrote:
What exactly does that mean? What does a parameter which doesn't exist
look like?

Private Sub Draw(Optional X As Variant, Optional Y As Variant)
If IsMissing(X) Then X = 720
If IsMissing(Y) Then Y = 2880
Cls
Circle (X, Y), 700
End Sub

Draw()
Draw(600)
Draw(600, 1500)
And what does that compile to in IL? Is "IsMissing" effectively the
same as "IsNothing" or whatever the VB terminology is? Does it work
for types other than Variant?

Jon

Jul 30 '07 #11
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@d55g2000hsg.googlegr oups.com...
And what does that compile to in IL? Is "IsMissing" effectively the
same as "IsNothing" or whatever the VB terminology is?
I have no idea - that's actually an old bit of VB code - no idea at all if
it would work in VB.NET... :-)
Does it work for types other than Variant?
It didn't in VB...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 30 '07 #12
On Jul 30, 4:42 pm, "Mark Rae [MVP]" <m...@markNOSPAMrae.netwrote:
And what does that compile to in IL? Is "IsMissing" effectively the
same as "IsNothing" or whatever the VB terminology is?

I have no idea - that's actually an old bit of VB code - no idea at all if
it would work in VB.NET... :-)
Ah. Looking in MSDN for VB.NET, it specifies:

<quote>
Every optional parameter in the procedure definition must specify a
default value.
</quote>

So at least in VB.NET, they *are* the same thing after all. That's a
relief in terms of my mental model, at the very least :)
Does it work for types other than Variant?

It didn't in VB...
I suspect that it was just a "default default" value which was the
equivalent of Nothing, or some singleton "missing value".

Jon

Jul 30 '07 #13
Jon Skeet [C# MVP] wrote:
On Jul 30, 4:42 pm, "Mark Rae [MVP]" <m...@markNOSPAMrae.netwrote:
>>And what does that compile to in IL? Is "IsMissing" effectively the
same as "IsNothing" or whatever the VB terminology is?
I have no idea - that's actually an old bit of VB code - no idea at all if
it would work in VB.NET... :-)

Ah. Looking in MSDN for VB.NET, it specifies:

<quote>
Every optional parameter in the procedure definition must specify a
default value.
</quote>
Yes, in fact "optional parameter" is a misleading term. I remember a
long thread back around 2000 in this ng, back when C# was new and some
people had a real issue with C# not having VB's concept of a so-called
"optional parameter" (and, indeed, VB.NET not supporting optional
parameters a la VB6). They aren't optional parameters but are hard-coded
default values.

IIRC, they are not fun to use because they are like a C# public const
variable vs. a readonly variable. With the former, the const value is
actually pulled out of the referenced assembly and inserted directly
into the consuming assembly; with the latter, a reference to the
readonly variable is deposited into the consuming assembly. Why is this
important. Well if you set your const to 12 and then change it to 13
then each and every assembly that references that public const must then
be recompiled to get the new 13 value. VB.NET's so-called optional
parameters work the same way. Avoid them! :-)

Here's what I could find with Google.

Real optional params (i.e., much of what has been discussed again in
this thread and real optional parameters with C#'s params keyword):

http://groups.google.com/group/micro...e95c94bf945e2d

Pointed to from a thread in this ng - calling COM objects that have
optional parameters:

http://support.microsoft.com/default...b;en-us;305814

And I joined in the fray here:

http://groups.google.com/group/micro...fa58605ebd935e

Lastly, another thread I just read points out that VB.NET optional
parameters are not CLS-compliant so, for example, an optional parameter
to a VB.NET method would only be optional to other VB.NET code, to C#
and other non-VB.NET languages the parameter would not be optional.

HTH.
--
-glenn-
Jul 30 '07 #14
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11*********************@w3g2000hsg.googlegrou ps.com...
And what does that compile to in IL? Is "IsMissing" effectively the
same as "IsNothing" or whatever the VB terminology is?

I have no idea - that's actually an old bit of VB code - no idea at all
if
it would work in VB.NET... :-)

Ah. Looking in MSDN for VB.NET, it specifies:

<quote>
Every optional parameter in the procedure definition must specify a
default value.
</quote>

So at least in VB.NET, they *are* the same thing after all. That's a
relief in terms of my mental model, at the very least :)
I couldn't agree more! I always hated that whole VB "sloppiness"...
Does it work for types other than Variant?

It didn't in VB...

I suspect that it was just a "default default" value which was the
equivalent of Nothing, or some singleton "missing value".
No doubt.
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 30 '07 #15
"G.Doten" <gd****@gmail.comwrote in message
news:Og**************@TK2MSFTNGP04.phx.gbl...
HTH.
Extremely interesting - thanks...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 30 '07 #16
"Paul Werkowitz" <ne********@primaprogramm.deschrieb im Newsbeitrag
news:1t*******************************@40tude.net. ..
Am Mon, 30 Jul 2007 13:02:26 +0100 schrieb Mark Rae [MVP]:
>>
It allows you to define different "versions" of the same function with
different signatures - other languages did something similar with
optional
parameters, but C# doesn't, thank God!
Optional parameters:
Why do you prefer

void f ( int data )
void f ( int data, bool withLogging )
over

void f( int data, bool withLogging = false )
And what about
void f(int data)
void f(int data, bool withLogging)
void f(int data, string loggingSource)

There are many similar examples in the CRL

Christof
Jul 31 '07 #17
"Paul Werkowitz" <ne********@primaprogramm.deschrieb im Newsbeitrag
news:1t*******************************@40tude.net. ..
Am Mon, 30 Jul 2007 13:02:26 +0100 schrieb Mark Rae [MVP]:
>>
It allows you to define different "versions" of the same function with
different signatures - other languages did something similar with
optional
parameters, but C# doesn't, thank God!
Optional parameters:
Why do you prefer

void f ( int data )
void f ( int data, bool withLogging )
over

void f( int data, bool withLogging = false )

???
Another Point:
In your second case the callee can't react on the missing parameter in a
more dynamic way, wich is possible with overloads aswell as with the VB
style optional parameters without default values.

Christof
Jul 31 '07 #18
Christof Nordiek wrote:
"Paul Werkowitz" <ne********@primaprogramm.deschrieb im Newsbeitrag
news:1t*******************************@40tude.net. ..
>Am Mon, 30 Jul 2007 13:02:26 +0100 schrieb Mark Rae [MVP]:
>>It allows you to define different "versions" of the same function with
different signatures - other languages did something similar with
optional
parameters, but C# doesn't, thank God!
Optional parameters:
Why do you prefer

void f ( int data )
void f ( int data, bool withLogging )
over

void f( int data, bool withLogging = false )
And what about
void f(int data)
void f(int data, bool withLogging)
void f(int data, string loggingSource)

There are many similar examples in the CRL

Christof

Nothing wrong with doing that!

The pattern I use for this type of situation is like this:

public void f(int data)
: this(data, false, null) {}

public void f(int data, bool withLogging)
: this(data, withLogging, null) {}

public void f(int data, string loggingSource)
: this(data, true, loggingSource) {}

public void f(int data, bool withLogging, string loggingSource) {}

These are all basically convenience overloads for the caller.

The first one means "call f and don't do any logging at all."

The second one means "call f and log or don't depending on the
withLogging parameter's value; if withLogging is true log to the default
log source."

The third is "call f and log or don't log depending on the withLogging
parameter's value, and further if withLogging is true then log to the
source specified in loggingSource (unless the caller explicitly
specified null in which case log to the default log source)."

And sometimes the 4th one is private if I don't want to expose that
variant of the constructor. I also use this same pattern with method
overloads quite frequently as well.

There is a bit of an art to coming up with good, useful overloads and
not just overloading for the sake of it.

--
-glenn-
Jul 31 '07 #19
Christof Nordiek wrote:
"Paul Werkowitz" <ne********@primaprogramm.deschrieb im Newsbeitrag
news:1t*******************************@40tude.net. ..
>Am Mon, 30 Jul 2007 13:02:26 +0100 schrieb Mark Rae [MVP]:
>>It allows you to define different "versions" of the same function with
different signatures - other languages did something similar with
optional
parameters, but C# doesn't, thank God!
Optional parameters:
Why do you prefer

void f ( int data )
void f ( int data, bool withLogging )
over

void f( int data, bool withLogging = false )

???

Another Point:
In your second case the callee can't react on the missing parameter in a
more dynamic way, wich is possible with overloads aswell as with the VB
style optional parameters without default values.

Christof

First off, VB.NET doesn't have optional parameters without defaults,
they must always have defaults. Are you referring to the optional
parameters as implemented by VB6 and earlier?

In either case, can you elaborate on what you mean by "can't react on
the missing parameter in a more dynamic way," since there are no missing
parameters in the f example, and I don't get what you mean. Thanks!

--
-glenn-
Jul 31 '07 #20
Dom
On Jul 30, 7:53 am, veebhudhi.chandramo...@gmail.com wrote:
Instead of using same name i can use different name,
is there any specific purpous is there to define the polymorphic
overloaded functions in c#
For the record, you shouldn't use the phrase "polymorphic overloaded
functions". You are confusing two very different concepts,
polymorphism (which is necessary for an Object Oriented language), and
overloading (which is not).

You are right to say that overloading just lets you reuse a name, but
that is actually a fairly big advantage. In my old job, we had a
library of reusable routines that anyone could contribute to. I
remember some of the important routines: Sort (which sorted an
integer array), InSort2 (which sorted a short array), CHSort (which
sorted strings), SortB (which sorted bytes), etc. It got messy. It
is much nicer now with overloading, and we can call of them "Sort".

Jul 31 '07 #21

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
28
by: David MacQuigg | last post by:
I'm concerned that with all the focus on obj$func binding, &closures, and other not-so-pretty details of Prothon, that we are missing what is really good - the simplification of classes. There are...
3
by: Lingyun Yang | last post by:
Hi, I want to use python as a "shell like" program, and execute an external program in it( such as mv, cp, tar, gnuplot) I tried: os.execv("/bin/bash",("/usr/bin/gnuplot",'-c "gnuplot <...
92
by: Reed L. O'Brien | last post by:
I see rotor was removed for 2.4 and the docs say use an AES module provided separately... Is there a standard module that works alike or an AES module that works alike but with better encryption?...
0
by: Merlin | last post by:
Hi, Can anyone explain why the following output text shows as if DLL's are loaded twice - the only difference is upper and lower cased text? I have a number of third party controls that show the...
2
by: sanu | last post by:
Hi, I have a dll declared in my VB.Net Webservice as below Declare Function fntest Lib "test.dll" _ Alias "TEST_FUNC" (ByVal a As Integer, ByVal b As Integer, ByVal c As Single) As Double ...
3
by: wolis | last post by:
Hi all, In an AJAX environment, is it possible to load and display some HTML and JavaScript and have that JavaScript functional? I have tried but the JavaScript functions dont appear to be...
9
by: noamsml | last post by:
Hi, I am new to python and am currently writing my first application. One of the problems I quickly ran into, however, is that python's imports are very different from php/C++ includes in the...
4
by: Dave | last post by:
I have a global.asax file with Application_Start defined and create some static data there and in another module used in the asp.net application and I realize that static data is shared amongst...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.