How to pass a class to a method

  • Thread starter Thread starter MrEd
  • Start date Start date
M

MrEd

Hi all, I need to pass a class to a method, (not an instance of a
class), I'll explain...
lets say we have a class, lets call it Shape (as the tipical example),
then lets say that i also have Circle, Rectangle and Triangle classes
that inherit from Shape.

what I need is to create a method on a helper class like this:

static public Shape[] DoSomething(<type of shape> classType, ...some
other params)
{
// do some loop according to other parameters
// create instances if what ever the classType is
classType newInstance = new classType();
//add them to the result array
 
Can you create your various Shape instances outside of the DoSomething
method? If you can do this then you can simply pass in a reference to
your objects and let virtual functions do the rest of the work for you.

Or if you don't like virtual functions you can use GetType to determine
the type of your instances (look up Object.GetType in MSDN).

Or if you really want to create all you objects in the DoSomething
function (not sure why you'd have to do this), you can just use an enum
to classify the supported class types - this is really, really bad OOD
though...
 
MrEd said:
So the question is, how do I declare that <type of shape> bit on the

Well, you use typeof(shape) :-)

Then in the helper class

(Shape)Activator.CreateInstance(classType)

Michael
 
I'm using virtual methods everywhere. and I love them, the third option
is really not an option as you say it is really bad OO.
Object.GetType does not help as I don't want to pass an instance, any
way it worked with the help of michael
 
Hi,

Not clear what you want, you have to know the precise type of the parameter
before calling the method, if you do so you could also pass an instance of
the very same type, unless you want to create more than one inside the
method, you better pass an instance.
 
MrEd said:
Michael, this is the ticket, it works!!! FANTASTIC!!!
thanks

I'm doing something similar in my app but considering the other posts I'm
questioning if it's the best method (I need to think about it a bit). One
thing I noticed is it's not possible to restrict the type passed in, whereas
if you pass in an instance you can restrict it to classes that inherit from
the same base. Someone mentioned that using an enum instead is considered
bad practice but what if the classes are private? Surely exposing the
classes even more of a poor practice if they really should be private.

Michael
 
Am I missing something here? Why not just use static public Shape[]
DoSomething(Shape classType, ...) ? Polymorphism 101
 
My apologies, I passed over your "I don't want to pass an instance".
It's Friday, that's my excuse.

Anyway, if you're using .net 2 (don't see a mention of your version but
I could have missed that too) this is a prime candidate for generics.

public abstract class Shape
{
public abstract string Greeting { get;}
}

public class Helper
{
public static T[] DoSomething<T>() where T : Shape,new()
{
return new T[]{new T(),new T()};
}
}
public class Circle : Shape
{
public override string Greeting {get{return "I'm a circle"; } }
}

Usage:

....
Circle[] circles = Helper.DoSomething<Circle>();
foreach (Circle c in circles)
{
Console.WriteLine(c.Greeting);
}
....
 
Back
Top