Array of classes

  • Thread starter Thread starter Chad Z. Hower aka Kudzu
  • Start date Start date
C

Chad Z. Hower aka Kudzu

I need to store a list of classes, not the instances of the classes. I had
planned to use an array, but can use a collection or some other form if more
appropriate.

The problem I am having is that I cannot find the C# syntax I need to
reference a class type.

public <class type>[] PowerTour = new <class type>[] {form1, form2};

Then later I will access this to get a list of classes which I will then
create with a call to new.

In Delphi you can do this with the "class of" construct. How can I do this in
C#?
 
Chad Z. Hower aka Kudzu said:
I need to store a list of classes, not the instances of the classes. I had
planned to use an array, but can use a collection or some other form if more
appropriate.

The problem I am having is that I cannot find the C# syntax I need to
reference a class type.

Just System.Type, and use the typeof operator:

Type[] foo = new Type[] {typeof(object), typeof(System.Windows.Forms)};

etc.

To find the type of a specific instance, call GetType on that instance.
 
Suppose you have a :
class myclass
{
public int f=0;
}



and somewhere in someother class you write:

myclass [] mc= new myclass[5]; //5 reference variables created which can
point to 5 instances/objects of type myclass
for (int i=0; i<mc.Length ;i++)
{
mc= new myclass();
mc.f= i;
}
public <class type>[] PowerTour = new <class type>[] {form1, form2};
the form1 and form2 is what you'll not write because that will intialize the
array during creation and which is what you dont want because:
Then later I will access this to get a list of classes which I will then
create with a call to new.




Chad Z. Hower aka Kudzu said:
I need to store a list of classes, not the instances of the classes. I had
planned to use an array, but can use a collection or some other form if more
appropriate.

The problem I am having is that I cannot find the C# syntax I need to
reference a class type.

public <class type>[] PowerTour = new <class type>[] {form1, form2};

Then later I will access this to get a list of classes which I will then
create with a call to new.

In Delphi you can do this with the "class of" construct. How can I do this in
C#?
 
Jon Skeet said:
Just System.Type, and use the typeof operator:

Type[] foo = new Type[] {typeof(object), typeof(System.Windows.Forms)};

Thanks. Is there any way to declare a type of my base class? Or is type
generic only? That is in Delphi I can do:

type
TMyClassType = class of TMyClass;

Then I can make an arry af TMyClassType, instead of just the basic class of
TObject. This makes it so I dont have to type cast when I pull them back out
of the array.

It appears though because of the use of typeof that types are implemented in
the copmiler quite a bit different in C#.
To find the type of a specific instance, call GetType on that instance.

Will file this for future use too. ;)
 
Chad Z. Hower aka Kudzu said:
Type[] foo = new Type[] {typeof(object), typeof(System.Windows.Forms)};

Thanks. Is there any way to declare a type of my base class? Or is type
generic only? That is in Delphi I can do:

type
TMyClassType = class of TMyClass;

Then I can make an arry af TMyClassType, instead of just the basic class of
TObject. This makes it so I dont have to type cast when I pull them back out
of the array.

Not sure what you mean here, to be honest...

There's only one type which represents a type, and that's Type. If you
want to declare an array of TMyClass, just use

TMyClass[] foo = new TMyClass[] {...};
It appears though because of the use of typeof that types are implemented in
the copmiler quite a bit different in C#.

Could you give the bigger picture here? What are you really wanting to
do, in the grand scheme of things?
 
Abubakar said:
myclass [] mc= new myclass[5]; //5 reference variables created which
can point to 5 instances/objects of type myclass

Thats not what I need. I need the class TYPES. Not actual instances of them.
 
Jon Skeet said:
Not sure what you mean here, to be honest...

There's only one type which represents a type, and that's Type. If you
want to declare an array of TMyClass, just use

When I reference something in foo, I will have to type cast it down to my
type to access any static methods in it correct? Because type is for object?

ie I assume I can do:

foo[0].<some static method in object>

or even:

object MyFoo = new foo[];

But to get items introduced in my decscendnat I will have to type cast
correct?

In Delphi you can declare "class types". That is typed versions of type,
which I can then make of my base class lower down, instead of at the object
level.

Is this possible in C#?
 
Also I am using an array. I looked around for a specialized collection of
objects, but there appears to be no such thing?
 
so just type Type instead of "myclass" or explain more :-)

Chad Z. Hower aka Kudzu said:
Abubakar said:
myclass [] mc= new myclass[5]; //5 reference variables created which
can point to 5 instances/objects of type myclass

Thats not what I need. I need the class TYPES. Not actual instances of them.
 
Chad Z. Hower aka Kudzu said:
object MyFoo = new foo[];

Actually I cant get this to go either. Now that I have my types in the array
- how can I create one of them? That is:

Foo[] FooList = new Foo[] {typeof(Foo1), typeof(Foo2)}

Now how can I perform a new on FooList[0] ?

Foo MyFoo = new Foo[0]() ?

No luck here....
 
Chad Z. Hower aka Kudzu said:
Jon Skeet said:
Not sure what you mean here, to be honest...

There's only one type which represents a type, and that's Type. If you
want to declare an array of TMyClass, just use

When I reference something in foo, I will have to type cast it down to my
type to access any static methods in it correct? Because type is for object?

ie I assume I can do:

foo[0].<some static method in object>

No. To call a static method, you need the actual type name. Note that
static methods aren't called polymorphically in any case - if one type
declares a static method with the same name as a static method in the
parent type, that's hiding rather than derivation.
or even:

object MyFoo = new foo[];

You can do that, but I don't think it does what you want it to...
But to get items introduced in my decscendnat I will have to type cast
correct?

In Delphi you can declare "class types". That is typed versions of type,
which I can then make of my base class lower down, instead of at the object
level.

Is this possible in C#?

No, there's nothing equivalent to what I think you mean.
 
Chad Z. Hower aka Kudzu said:
Also I am using an array. I looked around for a specialized collection of
objects, but there appears to be no such thing?

There's ArrayList for a generic list of object, or (to create your own
strongly typed collection) there's CollectionBase that you then need to
derive from.

Generics will provide simple strong collection typing.
 
Chad Z. Hower aka Kudzu said:
Chad Z. Hower aka Kudzu said:
object MyFoo = new foo[];

Actually I cant get this to go either. Now that I have my types in the array
- how can I create one of them? That is:

Foo[] FooList = new Foo[] {typeof(Foo1), typeof(Foo2)}

Now how can I perform a new on FooList[0] ?

Foo MyFoo = new Foo[0]() ?

No luck here....

I think you're looking for Activator.CreateInstance. Alternatively, use
GetConstructor on the Type, and then Invoke the constructor.
 
Jon Skeet said:
No. To call a static method, you need the actual type name. Note that
static methods aren't called polymorphically in any case - if one type
declares a static method with the same name as a static method in the
parent type, that's hiding rather than derivation.

Ack thats right - .net doenst support polymorphic statics... Anyways it wont
affect what I need to do.

I have a series of Foo classes. Lets call them Foo1, Foo2, .... They all
descend from Foo.

I need to store the TYPES in FooList (Any way I can, right now its an array).
I cannot store instances - they DONT exist yet.

Later on, I need to go through FooList and one by one instantiate the class
types listed in FooList.

From there I will operate on each instance one at a time.

How can I do this in C#?
 
Jon Skeet said:
There's ArrayList for a generic list of object, or (to create your own

Hmm will check that.
strongly typed collection) there's CollectionBase that you then need to
derive from.

Yes, but I didnt want to do that just for an object list. Surely this should
already be available in .net. :)
Generics will provide simple strong collection typing.

Generics will be great - but 2.0 features dont help me now. :)
 
Jon Skeet said:
I think you're looking for Activator.CreateInstance. Alternatively, use
GetConstructor on the Type, and then Invoke the constructor.

Thanks. I got it working. I must say that this very basic part of the
language/.net is VERY ugly and much more complicated than Delphi. ;( The
Delphi model is much simpler and still have all the same features. I hope
that .Net 2.0 addresses this a but - but I dont see how. The current
implementation appears to have backed them in a corner a bit. The only
other way I see is to create a "paralell" method to do this...

If anyone is interested....

public Type[] PowerTour = new Type[] {typeof(formMap)};

public void MoveToNextPowerTourForm(AppForm ACurrentForm) {
// This code is a bit ugly - dont let it scare you! This is
..net scariness and not
// IntraWeb scariness. It looks a bit ugly because of how
..net implements type references.
//
// Note that this is not the "normal" way to move between
forms.
// Normally to move between forms you can just call:
// MyNewForm.Show();
//
// MoveToNextPowerTourForm is used in this case because we
want to store them in a list
// for easy modification instead of hard coding them in
each form.
//
int LIndex = -1;
if (ACurrentForm != null) {
// Destroy the current form. We dont need it anymore
ACurrentForm.Release();

// Find the index of the current form
for (LIndex = 0; LIndex <= PowerTour.Length;
LIndex++) {
if (PowerTour[LIndex] == ACurrentForm.GetType())
{
break;
}
}
}

// if not the last one, then show it. If last one, then we
will
// just show the form that was before. IW does this because
// we released and did not show a new form.
if (LIndex < PowerTour.Length) {
AppForm LNextForm = (AppForm)Activator.CreateInstance
(PowerTour[LIndex + 1]);
LNextForm.Show();
}
}
 
Chad Z. Hower aka Kudzu said:
Ack thats right - .net doenst support polymorphic statics... Anyways it wont
affect what I need to do.

I have a series of Foo classes. Lets call them Foo1, Foo2, .... They all
descend from Foo.

I need to store the TYPES in FooList (Any way I can, right now its an array).
I cannot store instances - they DONT exist yet.

Later on, I need to go through FooList and one by one instantiate the class
types listed in FooList.

From there I will operate on each instance one at a time.

How can I do this in C#?

Type[] types = new Type[] {typeof(Foo1), typeof(Foo2) etc};

(Or use Type.GetType to load by name.)

Then:

Foo[] instances = new Foo[types.Length];

for (int i=0; i < types.Length; i++)
{
instances = (Foo) Activator.CreateInstance(types);
}

That's assuming you want to call a parameterless constructor, of
course. Hopefully from there you can work out how to call a constructor
with parameters etc - look at the overloads of
Activator.CreateInstance.
 
Chad Z. Hower aka Kudzu said:
Hmm will check that.


Yes, but I didnt want to do that just for an object list. Surely this should
already be available in .net. :)

If you don't mind it not being strongly typed (don't forget that the
objects themselves are strongly typed) then ArrayList is the way to go.
 
Back
Top