0

I have a method that takes a Generic parameter. I've restricted the type to one of two. Within the method I want to set call one of the two constructors.

Code:

internal static void CreateAddress<T>(out T address, bool isSave, int? cID) where T: Address_Accessor, Address
 {
   DateTime dt= DateTime.Now;
   int? eID = 1;
   int? sID = 50;

   if (typeof(T) == typeof(Address_Accessor))
     address = new Address_Accessor(dt, eID , sID);
   else
     address = new Address(dt, eID, sID);
}

The compile failure says:

Cannot implicitly convert type 'Address_Accessor' to 'T'. An explicit conversion exists (are you missing a cast?)

7
  • 3
    generics seem like a pretty terrible fit here. It seems like an overloaded method will do just fine. Commented Jan 25, 2012 at 14:48
  • Whats the relationship between Address_Accessor and Address? Commented Jan 25, 2012 at 14:49
  • This isn't proper, generics let you work with specific types, not just dynamically mix and match types. Commented Jan 25, 2012 at 14:49
  • 2
    Why would you have an out parameter in a method that doesn't otherwise return anything instead of just returning the address? Commented Jan 25, 2012 at 14:49
  • 1
    Maybe I'm wrong here, but are you trying to define that T can inherit from more than a class? C# doesn't support multi-inheritance. Commented Jan 25, 2012 at 14:54

2 Answers 2

1

As a straightforward solution (not having any details regardign method usage cases) if Address and Address_Accessor both represents some common entity - just introduce a common interface and restrict T to implement this interface, then you would be able insatntiating any of class which implements IAddress and setting to the IAddress reference (behind T).

interface IAddress
{
}

class Address_Accessor : IAddress
class Address : IAddress

internal static void CreateAddress<T>(out T address, bool isSave, int? cID) 
where T: IAddress  
{ 
}

I feel there some design issues around, could you please post a code which calling CreateAddress() method for both Address and Address_accessor cases? Perhaps you are looking for a some kind of abstract factory?

Sign up to request clarification or add additional context in comments.

2 Comments

Better yet, forget the generic entirely and just use IAddress instead of T.
This assumes a relevant link between the two types in a form only apparent for one of them (Address), if IAddress is being used for this purpose alone then this is a bad habit.
1

I don't understand why you need that code but you can convert result objects to T until return it:

object result = typeof(T) == typeof(Address_Accessor)
  ? (object) new Address_Accessor(dt, eID , sID)
  : (object) new Address(dt, eID, sID);

address = (T)result;

3 Comments

The assignment worked but error on the second one: address = (T)new Address(dt, eID, sID); I think I can just use: address = (T)new Address_Accessor(dt, eID , sID); without the if statement.
The other version is here, but you should think about changes in type hierarchy to solve this bug. My code is a round a way.
Different error: Type of conditional expression cannot be determined because there is no implicit conversion between 'Address_Accessor' and 'Address' -- I'm going to try a different approach for this code. Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.