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" 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
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"
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"
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
"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"
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"
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"
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"
"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"
"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"
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
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
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
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"
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"
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"
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
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
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"
"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"
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
>[...] 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"
"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" This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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...
| |