2

I have a class and 2 subclasses:

public class User
{
    public string eRaiderUsername { get; set; }
    public int AllowedSpaces { get; set; }
    public ContactInformation ContactInformation { get; set; }
    public Ethnicity Ethnicity { get; set; }
    public Classification Classification { get; set; }
    public Living Living { get; set; }
}

public class Student : User
{
    public Student()
    {
        AllowedSpaces = AppSettings.AllowedStudentSpaces;
    }
}

public class OrganizationRepresentative : User
{
    public Organization Organization { get; set; }

    public OrganizationRepresentative()
    {
        AllowedSpaces = AppSettings.AllowedOrganizationSpaces;
    }
}

I have created a data model to capture form data and to return the correct object type for the user:

public class UserData
{
    public string eRaiderUsername { get; set; }
    public int Ethnicity { get; set; }
    public int Classification { get; set; }
    public int Living { get; set; }
    public string ContactFirstName { get; set; }
    public string ContactLastname { get; set; }
    public string ContactEmailAddress { get; set; }
    public string ContactCellPhone { get; set; }
    public bool IsRepresentingOrganization { get; set; }
    public string OrganizationName { get; set; }

    public User GetUser()
    {
        var user = (IsRepresentingOrganization) ? new OrganizationRepresentative() : new Student();
    }
}

However, my ternary operation in the GetUser() method is failing with this error:

Type of conditional expression cannot be determined because there is no implicit conversion between {namespace}.OrganizationRepresentative and {namespace}.Student.

What am I missing?

gunr2171
  • 16,104
  • 25
  • 61
  • 88
drewwyatt
  • 5,989
  • 15
  • 60
  • 106
  • Both classes know nothing about each other but only commonality is the User base class. you should be doing a cast to User for which ever object you are trying to instantiated because you cannot create and assign an object different to a potentially other concrete object – Ahmed ilyas Jul 17 '14 at 18:10
  • http://stackoverflow.com/questions/828950/why-doesnt-this-c-sharp-code-compile – Habib Jul 17 '14 at 18:18

1 Answers1

6

You have to explicitly cast the first branch of the ternary expression to the base type (User) so that the compiler can determine what type the expression can evaluate to.

var user = (IsRepresentingOrganization) 
               ? (User)new OrganizationRepresentative()
               : new Student();

The compiler won't automatically deduce which base type should be used for the expression, so you have to specify it manually.

Adam Maras
  • 26,269
  • 6
  • 65
  • 91
  • 2
    Out of curiousity, why didn't you need to cast `new Student()` to `User` as well? – Alex Jul 17 '14 at 18:17
  • @Alex, only one of the operand of conditional operator could be cast to base class, – Habib Jul 17 '14 at 18:18
  • 3
    @Alex once you've specified the type of the first branch (`User`), the compiler can find an implicit conversion from the type of the second branch (`Student`) because it's a simple widening cast. – Adam Maras Jul 17 '14 at 18:18
  • To follow on from what Adam said, a ternary if can only return one type (or the types must be implicitly castable). That is to say, you can't do something like (a == true ? "X" : 5) - you would need to change it to (a == true ? "X" : "5"). See http://msdn.microsoft.com/en-GB/library/ty67wk28.aspx – ProgrammingLlama Jul 17 '14 at 19:15