Reputation: 5783
Good morning,
I have 2 objects that i need to work with from a third party. These objects are almost identical (at least the part i need to access are. Currently i have separate methods to handle each object and i would like to see, with the help of you kind folk, if i can convert this into a generics that can handle both objects.
public static ObjectOut Method1(Object1 obj1)
{
if (obj1.ErrorCode == 0)
{
//do something
}
}
public static ObjectOut Method2(Object1 obj2)
{
if (obj2.ErrorCode == 0)
{
//do something
}
}
If i convert this into a single generic method, how would i access the "ErrorCode" property of the object? Any help or guidance would be greatly appreciated.
Thanks
Upvotes: 1
Views: 177
Reputation: 172200
If both types of object have a common supertype, you don't even need generics:
public static ObjectOut Method(ICommonInterfaceWithErrorCodeProperty obj)
{
if (obj.ErrorCode == 0)
{
//do something
}
}
If the properties just happen to have the same name, you can refactor your code such that "common code" is in a separate method. I'll give you two examples:
DoSomething
is a big common block.DoSomething
needs to access properties of your objects.In the first case, you can just extract DoSomething
:
public static ObjectOut Method1(Object1 obj1)
{
if (obj1.ErrorCode == 0)
{
return DoSomething();
}
}
public static ObjectOut Method2(Object1 obj2)
{
if (obj2.ErrorCode == 0)
{
return DoSomething();
}
}
public static ObjectOut DoSomething() { ... }
In the second case, you extract all the values you need and pass them to your common method:
public static ObjectOut Method1(Object1 obj1)
{
return Method(obj1.ErrorCode);
}
public static ObjectOut Method2(Object2 obj1)
{
return Method(obj2.ErrorCode);
}
public static ObjectOut Method(int errorCode)
{
if (errorCode == 0)
{
...
}
}
If you need to execute methods on your objects, you can pass lambdas to Method
.
(And, obviously, you need to fix the fact that your methods don't return anything if the error code is non-zero. But I guess the compiler already told you that.)
Upvotes: 2
Reputation: 10257
i assume the 2 classes don't have a common base class / interface that holds all needed members...
what you can do is write a wrapper around those 2 objects that exposes all needed members and internally hands everything down to the object you are currently handling
something like
public class wrapper {
private ClassA objA;
private ClassB objB;
public wrapper(ClassA obj)
{
objA=obj;
}
public wrapper(ClassB obj)
{
objB=obj;
}
public int? Property1
{
get
{
return objA?.Property1 ?? objB?.Property1;
}
}
}
Upvotes: 0
Reputation: 77846
If they have any similar parent ... like an interface or abstract base class? then you can use where
constraint saying
public static ObjectOut Method1<T>(T obj1) where T : IBaseObject
{
if (obj1.ErrorCode == 0)
{
//do something
}
}
If not, then you will have to cast it and access accordingly like
public static ObjectOut Method1<T>(T obj1)
{
if(obj1 is Object1)
{
if (((Object1)obj1).ErrorCode == 0)
{
//do something
}
}
}
Upvotes: 0
Reputation: 7505
To do what you want, you need them to have a parent (class or interface) with the ErrorCode
defined. If you do, you could do a generic helper class such as :
public static class MyHelperClass<T> where T : TheCommonParent
{
public static ObjectOut Method(T obj)
{
if (obj.ErrorCode == 0)
{
//do something
}
}
}
If you want your ObjectOut to be different, you might need to have a 2nd generic type in the class definition, but there is not enough information based on your question to know for sure.
Upvotes: 0