Malfunction Posted August 13, 2004 Posted August 13, 2004 I have three classes inheriting from Base class: class Base class Child1 class Child2 class Child3 Now I need this method: void CreateNewInstance(Base someObject) { ActualType newObj = new ActualType(); } ActualType will either be Child1, 2 or 3 but how do I get it? Quote Debug me...
Administrators PlausiblyDamp Posted August 13, 2004 Administrators Posted August 13, 2004 It may be worth explaining why you need to do this - if a function is expecting any derived class then it can only safetly call functionality from the base class, why do you need to create a new instance of this variable within the function? You could always create a new instance and have the variable type set to Base, but declaring the variable type on the fly will involve an awful lot of effort. If you want to create an instance on the fly the following should work - I'm not convinced that this is the best approach to your problem though... void CreateNewInstance(Base someObject) { Type t= someObject.GetType(); Base newObj =(Base) System.Activator.CreateInstance(t, true); } Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Joe Mamma Posted August 13, 2004 Posted August 13, 2004 one technique is to enumerate you class hierarchy and have a static 'class factory' method off of the base: enum ClassType { class1, class2, class3 } public class BaseClass { public static BaseClass Create(ClassType aClass) { switch (aClass) { case class1: return new Class1(); case class2: return new Class2(); case class3: return new Class3(); } } } public class Class1: BaseClass{} public class Class2: BaseClass{} public class Class3: BaseClass{} public class ClassUser { public void Test() { BaseClass bc1 = BaseClass.Create(class1); BaseClass bc2 = BaseClass.Create(class2); BaseClass bc3 = BaseClass.Create(class3); } } Quote Joe Mamma Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized. Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
Malfunction Posted August 13, 2004 Author Posted August 13, 2004 one technique is to enumerate you class hierarchy and have a static 'class factory' method off of the base: ... thx geat solution ... Quote Debug me...
John_0025 Posted August 23, 2004 Posted August 23, 2004 (edited) Joe Mamma is correct. However I don't think the class factory should be in the baseclass. It doesn't make sense for a base class to contain information about classes that are derived from it. public interface iBaseClass{} public class Class1: iBaseClass{} public class Class2: iBaseClass{} public class Class3: iBaseClass{} public class ClassUser { public enum ClassType { class1, class2, class3 } public void Test() { iBaseClass bc1 = Create(ClassType.class1); iBaseClass bc2 = Create(ClassType.class2); iBaseClass bc3 = Create(ClassType.class3); } public iBaseClass Create(ClassType aClass) { switch (aClass) { case ClassType.class1: return new Class1(); case ClassType.class2: return new Class2(); case ClassType.class3: return new Class3(); default: return null; } } } p.s. This looks like a project where using an interface instead of a base class might be better. Edited August 23, 2004 by John_0025 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.