Passing a type as a parameter

  • Thread starter Thread starter Helen
  • Start date Start date
H

Helen

Hi

I want to write a function that searches a control's ancestors until
it finds a panel of a particular type. I need to do exactly the same
thing for three different types of panels, so I was wondering if it
was possible to pass a type to a function as a parameter?

Thanks :)

Helen
 
Helen said:
I want to write a function that searches a control's ancestors until
it finds a panel of a particular type. I need to do exactly the same
thing for three different types of panels, so I was wondering if it
was possible to pass a type to a function as a parameter?

Yup - just pass an instance of Type.
 
I just asked a similar question... you also need to use the typeof method
from the calling method

For instance:

public void FindPanel(Type panelType)
{
.... do the search ...
}

FindPanel(typeof(MyPanelTypeIAmSearchingFor));
 
Leicester B. Ford Jr. said:
I just asked a similar question... you also need to use the typeof method
from the calling method

For instance:

public void FindPanel(Type panelType)
{
... do the search ...
}

FindPanel(typeof(MyPanelTypeIAmSearchingFor));

Thanks for your help. That's just the information I was looking for :)

Helen
 
Jon said:
Yup - just pass an instance of Type.

While we're at it: is it possible to create typed versions of Type? So
that one can declare a variable that may only hold references to a
certain class type or types that descend from that type?

Christian
 
Christian Gudrian said:
While we're at it: is it possible to create typed versions of Type? So
that one can declare a variable that may only hold references to a
certain class type or types that descend from that type?

No, C# doesn't allow that level of type specification. You could create a
type-wrapping class that does the checking for you, but it will still be
entirely runtime checked.

Out of curiosity, what particulars scenarios would you use this?
 
Daniel said:
Out of curiosity, what particulars scenarios would you use this?

Admittingly, one rarely uses that kind of types, but sometimes they can
come in quite handy:

Imagine a Collection class to which only instances of the class
CollectionItem or one of its descendants may be added. Furthermore the
programmer should be able to specify which CollectionItem (or
descendant) exactly he wishes to add to that Collection.

Hence the implementation of the Collection class has to check for two
things:

1. Does the item which is about to be added descend from
CollectionItem?
2. Is the type of this item equal to the desired type?

With meta types the first check would already be done at compile time.

Christian
 
Christian Gudrian said:
Admittingly, one rarely uses that kind of types, but sometimes they can
come in quite handy:

Imagine a Collection class to which only instances of the class
CollectionItem or one of its descendants may be added. Furthermore the
programmer should be able to specify which CollectionItem (or
descendant) exactly he wishes to add to that Collection.

Hence the implementation of the Collection class has to check for two
things:

1. Does the item which is about to be added descend from
CollectionItem?
2. Is the type of this item equal to the desired type?

With meta types the first check would already be done at compile time.

This sounds alot like generics to me, which are coming in C# 2.0.
A generic class allows you to specify type parameters which you can use to
specify types throughout the class, for example

List<T> has the methods
Add(T item);
Remove(T item);
Insert(int index, T item);

List<string> has
Add(string item);
Remove(string item);
Insert(int index, string item);

Likewise, you could create your collection class:

public class Collection<T> where T : CollectionItem
{
Add(T item);
}

In this case, only CollectionItem and derived classes are valid types. So a
user could declare a variable typed as Collection<MyCollectionItem>, which
restricts that instance of Collection to allowing only MyCollectionItem and
derived classes.

Does that do what you want? I had taken your original question to mean being
able to specify a variable to be typed Type, but only accept a certain
subset of Type values.
 
Daniel O'Connell said:
This sounds alot like generics to me, which are coming in C# 2.0.
A generic class allows you to specify type parameters which you can use to
specify types throughout the class, for example

List<T> has the methods
Add(T item);
Remove(T item);
Insert(int index, T item);

List<string> has
Add(string item);
Remove(string item);
Insert(int index, string item);

Likewise, you could create your collection class:

public class Collection<T> where T : CollectionItem
{
Add(T item);
}

In this case, only CollectionItem and derived classes are valid types. So
a user could declare a variable typed as Collection<MyCollectionItem>,
which restricts that instance of Collection to allowing only
MyCollectionItem and derived classes.

Err, actually, no, that doesn't fit what you said, does it? You could use
generics to fake it like so:

int Add<V>(T item) where V : T
{
if (typeof(V)== typeof(T))
{
//doadd
}
else
return -1;
}

but I do not think that is a pattern to use terribly often. There is no way
to constrain a type based on its contents, just based on its actual type.
 
Daniel said:
Err, actually, no, that doesn't fit what you said, does it?

That's exactly what I was looking for. Thanks!

C# gets nicer and nicer. :)

Christian
 
Back
Top