Reputation: 1152
I am looking to design an interface method (a) that takes the parameters of another method (b), so if that method (b) changes, (a) will change also without me having to update it. Is this possible?
I'll try to explain with the following code
Method with parameters I'd like to call:
public int AddUser(string name, string description)
{
...
return userID;
}
I then have two methods,
public void AddCustomerUser(int customerID, int UserID)
{
//Do stuff
}
public void AddCustomerUser(int customerID, (string name, string description)<- these come from AddUser method?)
{
AddUser(name, description); <- these parameters come from what I'm trying to do
}
Is what am trying to achieve possible?
Upvotes: 1
Views: 66
Reputation: 186668
I'd rather implement (extract) a class for these parameters:
(string name, string description) -> class
The class itself could be something like that
public class User {
...
public String Name {...}
public String Description {...}
}
And methods will be
public int AddUser(User user);
public void AddCustomerUser(int customerId, User user);
So it's the class User
that can be changed and not methods.
You can go further and implement yet another class, CustomerUser
:
public class CustomerUser: User {
...
public int CustomerId {...}
}
And combine two methods into one:
public int AddUser(User user) {
CustomerUser cm = user as CustomerUser;
if (cm != null) {
int customerId = cm.CustomerId;
...
}
}
Upvotes: 7