Reputation: 22760
I have a class called Location
and it can either be Location<IClub>
or Location<IRestaurant>
.
I get a record from a database and that record then specifies whether this location is a restaurant or a club.
I need to create Location based on a string value within the record.
I have tried doing this;
object topLoc = null ;
if (record.type == "club")
topLoc = new Location<IClub>();
but I cannot access any of Location
's properties.
I also can't create the object in an if statement as when you leave the if the object will be out of scope.
Upvotes: 1
Views: 215
Reputation: 138181
It sounds like generics are not the tool you need. Rather than using generics to annotate the kind of location, consider using an enum
.
public enum LocationKind
{
Restaurant,
Club
}
Location location = new Location(LocationKind.Club);
// set up & use location as you see fit; expose the LocationKind through a
// property or something else along those lines
Upvotes: 2