question

Janet-5195 avatar image
0 Votes"
Janet-5195 asked TimonYang-MSFT commented

Pass different type of objects to the same method

Hi All,

I have a method commonMethod which takes Customer object as param. The logic is same for both Customer and Order class. Instead of rewriting the same code for Order class how to reuse it.

public class Customer
{
public int ID { get; set; }
public string Name { get; set; }
}

 public class Order
 {
     public int ID { get; set; }
     public string Product { get; set; }
 }


public string commonMethod(Customer c)
{
common logic for Customer and Order objects
}

dotnet-csharp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@Janet-5195
May I know whether your issue has been solved or not? If not, please share it in here. We can work together to figure it out.

0 Votes 0 ·
Viorel-1 avatar image
0 Votes"
Viorel-1 answered Viorel-1 edited

I think that it depends on details. For example, you can write this:

 public string commonMethod( Customer c)
 {
     return commonMethod( c.ID, c.Name);
 }
    
 public string commonMethod( Order o)
 {
     return commonMethod( o.ID, o.Product);
 }
    
 private string commonMethod( int ID, string text)
 {
     common logic . . .
 }

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered TimonYang-MSFT edited

You can consider using generics.

     public static class MyClass<T>
     {
         public static string CommonMethod(T t)
         {
             .......
         }
     }

If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered karenpayneoregon edited

Another option

 public class Base
 {
     public int Id { get; set; }
 }


Your classes

 public class Customer : Base
 {
     public string Name { get; set; }
 }
    
 public class Order : Base
 {
     public string Product { get; set; }
 }


Common class

 public class Operations
 {
     public static string Common(object sender) =>  sender switch
         {
             Customer customer => $"Customer id: {customer.Id} Name: {customer.Name}",
             Order order => $"Order id: {order.Id} Product: {order.Product}",
             _ => "Unknown"
         };
 }

Now the above is conceptual, this may make more sense if not use to a switch expression

 public static string Common(object sender)
 {
     var result = "";
     if (sender is Customer customer)
     {
         result = $"Customer id: {customer.Id} Name: {customer.Name}";
    
     }
     else if (sender  is Order order)
     {
         result = $"Order id: {order.Id} Product: {order.Product}";
     }
    
     return result;
 }

Test it

 Customer customer = new Customer() {Id = 1, Name = "Adams"};
 Debug.WriteLine(Operations.Common(customer));
    
 Order order = new Order() {Id = 12, Product = "Phone"};
 Debug.WriteLine(Operations.Common(order));

When using a switch expression or if statement you can call on say validation methods and or perform actions.


EDIT
Here the DoSomethingWith can be expanded to do whatever and if an unknown type is passed throw an exception


 public class Operations
 {
     public static string Common(object sender) => sender switch
     {
         Customer customer => DoSomethingWithCustomer(customer),
         Order order => DoSomethingWithOrder(order),
         _ => throw new Exception("Unknown type")
     };
    
     public static string DoSomethingWithCustomer(Customer customer) => $"Customer id: {customer.Id} Name: {customer.Name}";
     public static string DoSomethingWithOrder(Order order) => $"Customer id: {order.Id} Product: {order.Product}";
 }



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.