Click here to Skip to main content
15,898,987 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
Hello everybody

Old subject but with a twist - I searched and couldn't find answer to that. I know I can't use optional parameters with default values with web method so I have to use function overloading but...I saw examples of one optional parameter and I have around 10! If I understand well for 3 optional parameters I will need 7 overloaded functions (3 for 1 parameter, 3 for 2 and 1 for the whole 3) so for how many I need for 10? a lot! there must be a better way, no? and please don't tell me to use WCF - I can't switch to that now and I have to use WSDL

thanks a lot for helping
Posted
Comments
Sergey Alexandrovich Kryukov 30-May-13 10:50am    
Instead of "searching", you should apply common logic, at least sometimes.
—SA

When I have a scenario like this, I generally have one method that actually does the work and has ALL of the parameters. Then each of my overrides calls it, but sets an empty string or a zero or whatever default value to the parms that it doesn't use. This way you have your 10 different versions of the method, but you really only have to maintain one method with the code that really does the work. Like this:
VB
Public Overloads Sub MyMethod(ByVal strParm1 As String)
    MyMethodWork(strParm1, String.Empty, 0)
End Sub

Public Overloads Sub MyMethod(ByVal strParm1 As String, ByVal strParm2 As String)
    MyMethodWork(strParm1, strParm2, 0)
End Sub

Public Overloads Sub MyMethod(ByVal strParm1 As String, ByVal strParm2 As String, ByVal intParm3 As Integer)
    MyMethodWork(strParm1, strParm2, intParm3)
End Sub

Private Sub MyMethodWork(ByVal strParm1 As String, ByVal strParm2 As String, ByVal intParm3 As Integer)
    'Do actual work here.
End Sub

Alternatively, you could just always call a method that has ALL of the parms and set default values when you are calling it. I do this more often than using overloads.

----- Update ----
You could also add these methods:
VB
Public Overloads Sub MyMethod(ByVal intParm3 As Integer)
    MyMethodWork(String.Empty, String.Empty, intParm3)
End Sub

Public Overloads Sub MyMethod(ByVal strParm1 As String, ByVal intParm3 As Integer)
    MyMethodWork(strParm1, String.Empty, intParm3)
End Sub


But you can't have a method that passes strParm1 and intParm3 and then have ANOTHER method that passes strParm2 and intParm3. Because both methods would have a string parm and an integer parm so it won't know which one you are trying to call. So any combination as long as you don't double up on the data types.
 
Share this answer
 
v4
Comments
g77777 31-May-13 4:15am    
Thanks a lot mate, If I understood you well my problem still remains - if I want to call the function with just intParm3? with intParm3 and strParm1? etc. Am I missing something?
Sergey Alexandrovich Kryukov 31-May-13 8:15am    
Yes, you are missing... hard to say what. Probably the whole idea of the method...
—SA
Kschuler 31-May-13 8:50am    
I updated my answer with some more info. Hope it helps.
Sergey Alexandrovich Kryukov 31-May-13 8:15am    
Quite a right and simple idea, my 5.
—SA
Please see my comment to the question.

Bad term was coined quite a while ago, "overloading". Too many beginners have been highly confused. Nothing is actually "overloaded", just because nothing is "loaded". This aspect of syntax does not need a special term at all. It's just this: different, possibly even unrelated methods are allowed to use the same name, as soon as it is possible for a compiler to tell on from another by the call statement. Naturally, the parameter lists should be "different enough", otherwise a compiler won't be able to resolve the method to be actually called. And if you also take into account such things as inheritance, you will see that even with legitimate method declarations, the call statement may be written in a ambiguous way. (Do I even have to explain it, again and again? Such examples are trivial, as well as the possible resolutions.)

No, I don't sat you are confused with the term "overloading". You are confused with something more weird. Having a possibility to declare many combinations of parameters in the parameter lists does not mean you have to declare then all. Moreover, it would only cause troubles..

You can create as many methods as it seems to be useful for the using of your type, no more.

—SA
 
Share this answer
 
v2
Comments
g77777 31-May-13 4:18am    
Sergey, your "answer" doesn't contribute anything to anyone except maybe your ego. This is a forum for people who want to help people not to take out aggression and frustration from life so spare us your "deep insights" and get some help.
Sergey Alexandrovich Kryukov 31-May-13 8:11am    
Do you have problems?.. :-)

Look, you are apparently unable to resolve the simplest software problem, and I provide you a complete solution which you, by some reason, cannot even get, but you are sitting here and talking about "ego". Have you ever heard of "shifted activity" in ethology?..

—SA
Kschuler 31-May-13 8:55am    
Sorry, SA. But g77777 has a point. His question is basically that he doesn't understand the concept and you've made it seem like that's not a valid question and he's an idiot for even thinking of it. It is a valid question. So why not put together some of the nice links to sites that explain it like you do with other questions I've seen you answer?
Sergey Alexandrovich Kryukov 31-May-13 9:16am    
I don't say it's invalid, but the solution is to explain the misconception. What else do you think I should explain? Sorry if I failed to carry the idea clearly, but the this is a right idea. This is no about references.

Here is the whole point. "Overloading" is not something that should be explained as a special feature or technique. Essentially, there is no "overloading". Let's say, I simply don't know good links where it is explained properly, so I always try to explain it by myself. What should probably be explained is the way to resolve ambiguity, but this is not the OP's problem. You own advice is good enough, but somebody could help to explain the misconception. Don't you see it?

If it wasn't clear, OP could ask questions, but he started to attack on personal level instead. Do you think it should be tolerated? There is nothing personal or offending in pointing out to someone's confusion, without personal characteristics. Everyone could see that if it happens to me, I say thank you and try to fix myself. (Or provide argument to maintain my opinion.)

—SA
Kschuler 31-May-13 9:28am    
You said "I have no idea how anyone can come to such a strange idea."...that's just a bit condescending.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900