Reputation: 2650
I have two classes named ROAD and PATH
public class ROAD
{
public string getData()
{
return "Marlton Road";
}
}
public class PATH
{
public string getData()
{
return "Tagore Path";
}
}
Also i have a function named FETCH() in my Static Void Main
FETCH() contains following code
public returnType FETCH(bool flag)
{
if(flag)
{
ROAD obj=new ROAD();
return obj;
}
else
{
PATH obj=new PATH();
return obj;
}
}
Now my question is what should be the return type of function FETCH(). Or is there any other way to implement this logic.
Upvotes: 3
Views: 144
Reputation: 4902
using interface would be the best way:
public interface IData
{
string getData();
}
public class ROAD : IData
{
public string getData()
{
return "Marlton Road";
}
}
public class PATH : IData
{
public string getData()
{
return "Tagore Path";
}
}
public IData FETCH(bool flag)
{
if (flag)
{
ROAD obj = new ROAD();
return obj;
}
else
{
PATH obj = new PATH();
return obj;
}
}
Upvotes: 1
Reputation: 28738
It's object
. If you want it to be something else, you just have to have those classes share a common base or implement a common interface. For example:
public class Road : IPavedSurface
{
// members
}
public class Path : IPavedSurface
{
// members
}
// then
public IPavedSurface Fetch(bool flag)
{
Upvotes: -2
Reputation: 18633
How about this:
interface IWithData
{
string GetData();
}
class Path: IWithData
{
public string GetData()
{
return "Tagore Path";
}
}
class Road: IWithData
{
public string GetData()
{
return "Marlton Road";
}
}
class SomeOtherClass
{
// ...
public IWithData FETCH(bool flag)
{
if(flag)
{
Road obj=new Road();
return obj;
}
else
{
Path obj=new Path();
return obj;
}
}
// ...
}
Upvotes: 0
Reputation: 22255
I suggest you create an interface that both PATH and ROAD implement (e.g. IGetData) then have both classes implement it, and have FETCH return an IGetData.
Upvotes: 4
Reputation: 1503649
It would have to be object
in this case, as PATH
and ROAD
have no common base type. (They also don't follow .NET naming conventions - something which should be fixed, along with your getData
method, and your FETCH
method. Even in sample code, it's worth trying to make your names follow the normal conventions).
Consider making the two classes implement an interface or give them a common base class. That common type could then be the return type of the method. It looks like you could probably have an interface with your getData
method in, for example. Hopefully in your real classes it could have a more meaningful name - something to do with both paths and roads.
Upvotes: 7
Reputation: 54640
Object
, and then you cast to ROAD
or PATH
. Or, if they share a common base class, return that. See also.
But you probably don't want to do this. ROAD and PATH should each have their own static factory method:
class ROAD {
static ROAD newRoad() { return new ROAD(); }
}
class PATH {
static PATH newPath() { return new PATH(); }
}
Or some other, better pattern, depending on why you're doing it this way.
Upvotes: 2