?
Solved

C#  --- Just a quick lesson on Overloading a function and related questions.

Posted on 2005-03-15
7
Medium Priority
?
195 Views
Last Modified: 2010-04-16
Hi everyone,

I was wondering... I've read some on overloading but had just a quick set of questions.

I am simply looking to create a method/function that can take different Params/arguments.  So...

Do I need to re-write the whole function/method like this

public void MyFunction(string MyStringInput, bool myBool)
{
code here...
}

and...

public void MyFunction(string[] MyStringInputArray, bool myBool)
{
code here...
}

I understand that which ever best overload is picked based on the types passed in but...

1)  Do I need 2 independant and separate functions/methods for an overload or can I just use one?   For example if the "Code here" inside the function is written to handle either type of "type thats passed in, is there a way to just overload using one function, i.e., something like...

public void MyFunction(string MyStringInput | string[] MyStringInputArray, bool myBool)
{
code here...
}

or is there no way around having 2 different functions with the same name, and almost duplicate code?

Let me thank you in advance and sorry for the seemingly trivial nature of my question.

Hope all is well in your world.

Regards,

Dynotoe




0
Comment
Question by:Dynotoe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 8

Expert Comment

by:gena17
ID: 13547356
Hi, Sean.

Yes you need to define two different methods for overloading.
Each method knows exactly what are the types of its arguments.

You can use one method that gets some common type argument (like Object, for example),
determine what is the type of a passed parameter and deal with it according to its type.
However, I don't recomment this approach.

To remove duplicate code - there are a lot of techniques and it is impossible to cover all here, expecially without knowing the business logic of your project.
But you can create a method that contains the same code of your two overloaded methods and call to it from both.

Hope this helps,

Gena
0
 
LVL 9

Expert Comment

by:Wim_Bl
ID: 13547891
Hi,

you might also consider using delegates. I'm not familiar with them myself but I know that their purpose is that you can
e.g. say: doSomething(thisFunction). Then, the doSomething function takes care of deciding which actual function to use.
Might be helpful, but Gena's solution is just as helpful, and probably easier to implement.

greetings
0
 

Author Comment

by:Dynotoe
ID: 13548043

Hey Gena,

Thanks for the input.

In an overloaded function does the declaration look different or is it the same, i.e.,...

"scope"(public, private...etc) then return type (void, string[]...etc) name........?

"You can use one method that gets some common type argument (like Object, for example),
determine what is the type of a passed parameter and deal with it according to its type.
However, I don't recomment this approach."

Why wouldn't you resommend this?  Sounds pretty great to me.  What are the drawbacks or bad aspects to this?

What would the code look like in a function that takes a plain "object" to then determine if it is a string or string[] array for example just so I can see how it is done.

In your last paragraph do you mean a "Y" like structure function/method call where there is very little code in both upper "tips" "functions of the "Y" structure/shape???

Best,

Dynotoe
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 13

Assisted Solution

by:dungla
dungla earned 500 total points
ID: 13550666
Dynotoe, why don't you use an abstract class to write your code first (for example MethodB(string s1, int i1), then you can inherited from this abstract class. In your deriveds class, you can override MethodB to take another parameter MethodB(string s1, int i1, bool b1) and still able to use MethodB without write any code again.

In your implement method, you can use base.MethodB first to call MethodB from abstract class to get code in MethodB executed then do the other thing when finished:

public abstract class MyB
public virtual int MethodB(string s1, int i1)
{
return i1 + 2;
}
}

public class MyBI : MyB
{
public override int MethodB(string s1, int i1, bool b1)
{
if (base.MethodB(s1, i1) > 5)
{
i1++;
}
return i1;
}
}
0
 
LVL 3

Assisted Solution

by:brckoslav
brckoslav earned 500 total points
ID: 13552942
Or

if all methods have same meaning, you can write "biggest method" that have "universal" parameters and other methods invoke this method

from your question

public void MyFunction(string MyStringInput, bool myBool)
{
  // no code just call biggest function
  MyFunction(new String[] {MyStringInput}, myBool);
}

and...

public void MyFunction(string[] MyStringInputArray, bool myBool)
{
code here...
}
0
 
LVL 8

Accepted Solution

by:
gena17 earned 1000 total points
ID: 13553918
Sean,

Overloaded method's declaration looks different.
You can not give ONLY different return types or ONLY different scope.
Say, you have:
  private void f() { ... }
and
  private int f() { ... }
What should system do in a case you calling f() ? There is no way to know what method do you want.
Thus this is incorrect.
The same thing about the ONLY scope difference.

Take a look:
private void f (object o)
{
  if ( o is string )
    // do something
  else
    // do something else
}
In this case you determine the type of passed parameter in runtime. This is not recommended way. Because there are a lot of "nice" approachs to deal with such situations.
For example see the post of brckoslav or dungla.

I know that this is complex and I try to give simle answers :)
What is "Y" structure?

P.S. This is complex question to answer because we don't know the purpose of this part of your project.

All the best,

Gena
0
 

Author Comment

by:Dynotoe
ID: 13556676

Hi everyone,

Thanks for all your input.

Wim -- Thx.
dungla -- A little over my head at this point in my developement.  I'm still getting my head around the "Normal" Class lol :)  Plus I don't want to just start adding new classes for otherwise simple tasks, but thanks very interesting.

brockoslav -- Nice example.  This is what I refered to as a "Y" shaped function call.  The funvction with the "Meat" at the bottom of the "Y"

Gena - Thanks again!

Best,

Dynotoe
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question