473,325 Members | 2,860 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,325 software developers and data experts.

Array of classes

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 (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"
Nov 15 '05 #1
24 10479
Chad Z. Hower aka Kudzu <cp**@hower.org> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
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[i]= new myclass();
mc[i].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" <cp**@hower.org> wrote in message
news:Xn******************@127.0.0.1... 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 (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Nov 15 '05 #3
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
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 (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"
Nov 15 '05 #4
Chad Z. Hower aka Kudzu <cp**@hower.org> wrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5
"Abubakar" <em**********@yahoo.com> wrote in
news:#x**************@TK2MSFTNGP12.phx.gbl:
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 (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"
Nov 15 '05 #6
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
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#?

--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"
Nov 15 '05 #7
Also I am using an array. I looked around for a specialized collection of
objects, but there appears to be no such thing?
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"
Nov 15 '05 #8
so just type Type instead of "myclass" or explain more :-)

"Chad Z. Hower aka Kudzu" <cp**@hower.org> wrote in message
news:Xn******************@127.0.0.1...
"Abubakar" <em**********@yahoo.com> wrote in
news:#x**************@TK2MSFTNGP12.phx.gbl:
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 (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Nov 15 '05 #9
"Chad Z. Hower aka Kudzu" <cp**@hower.org> wrote in
news:Xn******************@127.0.0.1:
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 (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"
Nov 15 '05 #10
"Chad Z. Hower aka Kudzu" <cp**@hower.org> wrote in
news:Xn******************@127.0.0.1:
Foo MyFoo = new Foo[0]() ?


Sorry, FooList[0]()

But still doesnt work.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"
Nov 15 '05 #11
Chad Z. Hower aka Kudzu <cp**@hower.org> wrote:
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #12
Chad Z. Hower aka Kudzu <cp**@hower.org> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #13
Chad Z. Hower aka Kudzu <cp**@hower.org> wrote:
"Chad Z. Hower aka Kudzu" <cp**@hower.org> wrote in
news:Xn******************@127.0.0.1:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #14
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
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#?
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"
Nov 15 '05 #15
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in news:MPG.1a9d7c1a6764329798a193
@msnews.microsoft.com:
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. :)
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"
Nov 15 '05 #16
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
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 (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"
Nov 15 '05 #17
Chad Z. Hower aka Kudzu <cp**@hower.org> wrote:
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
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#?


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[i] = (Foo) Activator.CreateInstance(types[i]);
}

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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #18
Chad Z. Hower aka Kudzu <cp**@hower.org> wrote:
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in news:MPG.1a9d7c1a6764329798a193
@msnews.microsoft.com:
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. :)


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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #19
Can u write a little delphi code to do what u r saying ?

"Chad Z. Hower aka Kudzu" <cp**@hower.org> wrote in message
news:Xn******************@127.0.0.1...
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
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#?
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Nov 15 '05 #20
"Abubakar" <em**********@yahoo.com> wrote in news:OL2xnqi9DHA.2656
@TK2MSFTNGP11.phx.gbl:
Can u write a little delphi code to do what u r saying ?


I've solved it with Jon's help. Did you want the Delphi code just for
curiosity? If so I can write it.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"
Nov 15 '05 #21
Hi Chad,
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;


No, C# (and .NET as a platform)doesn't support the concept of meta classes
as Delphi does. Eventhough sometimes you can think of .NET Type objects as
Delphi meta classes this could be correct in some very-very few situations.
And because you don't have the Delphi concept of meta classes you don't have
virtual constructors and virtual static members.

B\rgds
100


Nov 15 '05 #22
>[...] Did you want the Delphi code just for
curiosity? If so I can write it. yes for my own learning I want to look at how its done in delphi. Thanx :-)
"Chad Z. Hower aka Kudzu" <cp**@hower.org> wrote in message
news:Xn******************@127.0.0.1... "Abubakar" <em**********@yahoo.com> wrote in news:OL2xnqi9DHA.2656
@TK2MSFTNGP11.phx.gbl:
Can u write a little delphi code to do what u r saying ?


I've solved it with Jon's help. Did you want the Delphi code just for
curiosity? If so I can write it.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Nov 15 '05 #23
"Abubakar" <em**********@yahoo.com> wrote in news:uOkRxvu9DHA.3428
@tk2msftngp13.phx.gbl:
[...] Did you want the Delphi code just for
curiosity? If so I can write it.

yes for my own learning I want to look at how its done in delphi. Thanx

:-)

This is just in the newsreader, so it might not compile but here is the
basic idea:

type
TFoo = class(TObject)
...
...
...
end;

TFoo1 = class(TFoo)
...
...
...
end;

TFoo2 = class(TFoo)
...
...
...
end;

TFooClass = class of TFoo;

Now I can make an array:

var
MyFoos: Array[0..1] of TFoo = (TFoo1, TFoo2);

Then later I can do

var
LFoo: TFoo;
begin
LFoo := MyFoos[0].Create;

Make sense? Its a lot cleaner in Delphi. :)
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"
Nov 15 '05 #24
I did not and still do not understand your requirement despite reading
almost all the posts. But I will venture to give the following
suggestion

1. Create a class indexer
public class myIndexer
{
object[] o=new object[12];
public object this[int index]
{
get
{
return o[index];
}
set
{
o[index]=value;
}
}
}

2.
Assign the form1,form2 objects to the above class
myIndexer ob=new myIndexer();

ob[0]=form1;
ob[1]=form2;

3.

Convert them to form objects with

form1=(Form)ob[0];

etc.

I hope the above is what you have been looking for.

with regards,
J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #25

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

Similar topics

3
by: Phil Powell | last post by:
if (is_array($_POST)) { foreach ($this->getAssocSectionsObjArray($key, $dbAP) as $obj) { print_r($obj); print_r(" in array? "); print_r(in_array($obj, $result)); print_r("<P>"); if...
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
4
by: Bill | last post by:
I would like to create a static array of classes (or structs) to be used in populating name/value pairs in various WebForm drop down list boxes, but am not quite sure of the construct (or rather to...
2
by: david | last post by:
Well, as a matter of fact I_HAD_MISSED a basic thing or two, anyway, although Ollie's answer makes perfectly sense when dealing with classes, it doesn't seem to me to apply as well if you have to...
7
by: Ron | last post by:
Hello, I have 4 classes that use 4 DTS packages on 4 different tables. So I have Dim cls1 As New clsDTS1, cls2 As New clsDTS2 Dim cls3 As New clsDTS3, cls4 As New clsDTS4 Each class has a...
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
7
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
14
by: mm | last post by:
How can I do a array of class? s1= ## this array should hold classes ## class definition class Word: word="" ## empty words... INIT
11
by: C C++ C++ | last post by:
Hi all, got this interview question please respond. How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Rgrds MA
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.