I have an abstract base class "UserFeedback", and a couple of subclasses "A" and "B".
UserFeedback has a static "CreateInstance()" method.
I'd like to PREVENT callers from instantiating a new UserFeedback object directly:
public abstract class UserFeedback
{
public string ComponentType { get; set; }
public string UserName { get; set; }
public string EMail { get; set; }
protected UserFeedback(string componentType)
{
ComponentType = componentType;
}
public static UserFeedback CreateInstance(string componentType)
{
switch (componentType)
{
case "A":
return new A(componentType); // Line 32
case "B":
return new B(componentType); // Line 34
default:
throw new ArgumentException(String.Format("User Feedback Type: {0}", componentType));
}
}
public abstract string GetTitle(string operation);
}
public class A : UserFeedback
{
protected A(string componentType) : base(componentType)
{ }
public override string GetTitle(string operation)
{
return "A Feedback";
}
}
public class B : UserFeedback
{
protected B(string componentType) : base(componentType)
{ }
public override string GetTitle(string operation)
{
return "B Feedback";
}
}
This gives me compile errors on Line 32 and 34:
Error CS0122 'A.A(string)' is inaccessible due to its protection level
Is there any "simple" way to accomplish this in newer versions of C#/.NET?
Or should I just punt, and make the constructors for A() and B() "public"?