Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

Debug me...
  • Administrators
Posted

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);
}

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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);
 }
}

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.

  • 2 weeks later...
Posted (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 by John_0025

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...