Click here to Skip to main content
15,900,325 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can some one please suggest me any approach other than method overriding

Example

Method1:


public int search (int id)
{
search in data base1
}

Method 2:


public int search (int id)
{
search in data base2
}
Posted
Updated 6-Apr-15 4:55am
v3

Well, you could use different names:
C#
public int searchDB1 (int id)
{
search in data base1
}

public int searchDB2 (int id)
{
search in data base2
}

Or you could have one method, and pass the database to it:
C#
public int search (int id, SqlConnection con)
{
search in data base
}

It really depends on what you are trying to do, and why.
For example, it might be worth passing the appropriate Search method as a delegate parameter to the calling method:
C#
public void DoSomethingIncludingSearch(Func<int> search)
{
    ...    
    search(userId);
    ... 
}
And then call it with the appropriate method:
C#
public void DoSomething()
{
    ...
    DoSomethingIncludingSearch(searchDB1);
    ...
    DoSomethingIncludingSearch(searchDB2);
}
 
Share this answer
 
There is no such thing as "method overriding". What you are talking about have nothing with "overriding", which is the heart of OOP and is only possible with virtual methods and inheritance.

This is as simple as this: methods are allowed to have different names as soon as the compiler has a way to determine what exactly should be called by the calling code. In your case, this is totally impossible. It would be possible if your methods with the same name had different signatures. Even then, it could be a problem, is the signatures are similar. For example, one parameter could be assignment-compatible with another. Just place yourself in the place of the compiler and try to think: how obvious the choice of one of the method at the call?

This is usually called "method overloading", but I don't know more unfortunate and confusing term that that: Some Programming Approaches to "Neuro-Linguistic Programming", 3. Terminological Methods.

—SA
 
Share this answer
 

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