Reputation: 18343
I have function that do common stuff:
class MyBaseClass<TOut>
{
public abstract TOut MyFunc<TOut>();
}
and particular implementations
class MyDerivedClass : MyBaseClass<string>
{
public override string MyFunc<string>()
{
string strSomeThing;
... initialization of strSomeThing;
return strSomeThing;
}
}
and few similar derived classes.
(There are no any restrictions on generic type).
Today I got case when I need 'MyFunc' to do some actions (let's say save data in DB) and do NOT need to return anything meaningful...
For this case I would like to have 'void' as a type parameter...
I tried to do that, compiler answered:
Cannot use 'void' as a type parameter.
Could you please advise, what is the best solution for my case?
Just use 'Object' as parameter type and return null in function?
Is there something better?
Thanks a lot!
Upvotes: 2
Views: 1487
Reputation: 241601
This is a huge smell. Whatever is going on that you have defined a generic class that has a method that is returning objects and you want to derive from that class and not return anything suggests that something is very wrong. It's very likely that this method is doing something different in various overloads (some overloads are saving to a database, and some overloads are sending a job to the printer, and some overloads are constructing an object, or whatever) and that's bad design.
Please elaborate on your use case but my gut feeling is that a redesign is the best solution to your problem ("Doc, it hurts when I do this." "Then stop doing that.")
Upvotes: 0
Reputation: 887305
Standard practice is to create a separate non-generic type for this case.
EDIT: For example:
class MyBaseClass
{
public abstract void MyFunc();
}
Depending on how you use them, you may want to make them share an interface or base class.
Upvotes: 1
Reputation: 2454
You could typedef a "_void" to short int or something like that and use that for readability purposes. (and just return 0).
Upvotes: 0