473,408 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

How to pass a class to a method

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
Apr 7 '06 #1
8 1671
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...

Apr 7 '06 #2
"MrEd" <ed*********@gmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
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
Apr 7 '06 #3
Michael, this is the ticket, it works!!! FANTASTIC!!!
thanks

Apr 7 '06 #4
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

Apr 7 '06 #5
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.
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"MrEd" <ed*********@gmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
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
.
.
.
}

so at a later stage I can say...

Circle[] someCircles = DoSomething(Circle, ....);
Triangle[] someTrienagles = DoSomething(Triangle, ...)
etc.

So the question is, how do I declare that <type of shape> bit on the
method so it is restricted to accept classes of type Shape.

Thanks for any responses

Apr 7 '06 #6
"MrEd" <ed*********@gmail.com> wrote in message
news:11********************@i40g2000cwc.googlegrou ps.com...
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
Apr 7 '06 #7
Am I missing something here? Why not just use static public Shape[]
DoSomething(Shape classType, ...) ? Polymorphism 101

Apr 7 '06 #8
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);
}
....

Apr 7 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Jay Douglas | last post by:
Hello, I am needing to pass a class object (this) by reference to a method in a different class. When I do the following code I get the error (Cannot pass '<this>' as a ref or out argument because...
13
by: Francois Appert | last post by:
This post was originally in the C# Corner site, but their server is down. I'd like to see if this group can answer. I program in C++ and am learning C#. The issue is: why should anybody...
9
by: raylopez99 | last post by:
I'm posting this fragment from another thread to frame the issue clearer. How to pass an object to a function/method call in C# that will guarantee not to change the object?* In C++, as seen...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
21
by: raylopez99 | last post by:
In the otherwise excellent book C# 3.0 in a Nutshell by Albahari et al. (3rd edition) (highly recommended--it's packed with information, and is a desktop reference book) the following statement is...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.