By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,007 Members | 979 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,007 IT Pros & Developers. It's quick & easy.

Defining methods for a class that are accessible when declaring arrays of that class???

P: n/a
Hello,

this may seem a strange question, but is there a way of being able to call
methods of a class through an array of that class when not referencing a
specific object in the array.
In other words, defined a class class_A I'd like to be able to do the
following:

// defining an array of class_A objects
class_A myArray[] = new class_A[10];

// calling MyMethod on the array
myArray.MyMethod();

where MyMethod is something I defined (somehow).

Is there a way to obtain such a behavior???
Thx.

Bob Rock


Jul 21 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"Bob Rock" <no***************************@hotmail.com> wrote:
this may seem a strange question, but is there a way of
being able to call methods of a class through an array of
that class when not referencing a specific object in the array.


I suppose you want a static method (one that can be called using the
class name rather than a specific instance), but this has nothing in
particular to do with arrays.

What exactly are you trying to achieve?

P.
Jul 21 '05 #2

P: n/a
> I suppose you want a static method (one that can be called using the
class name rather than a specific instance), but this has nothing in
particular to do with arrays.

What exactly are you trying to achieve?

P.


Static methods are available on classes.
This is what I need to do:
1) define a class.
2) create an array of objects of this class.
3) define methods on the array ..... NOT methods on a specific object in the
array (instance methods - MyArray[1].MyMethod()), not on the class (class or
static methods - MyClass.MyMethod()) but on the array (MyArray.MyMethod()).

public class MyClass
{

}

public class StartupClass
{
public static main()
{
MyClass myArray = new MyClass[10];
myArray.MyMethod(); // this is what I'd like to have
}
}

What I'd like to do is be able to define MyMethod(). A method that is
available on the array, not on the class or on an element/object of the
array.
Hope this clarifies my intent.

Bob Rock
Jul 21 '05 #3

P: n/a
This is not supported. The problem is you want to use an array of objects as
the object instance in the method call, and only a single instance can be
specified. This instance is used as the object instance in the method body.
In other words, if your class defined a field, one per instance, then each
object in the array would have its own copy of that field. In the method
itself, if there was a reference to the field then it would be impossible to
determine which object instance you meant.

Instead, you could define a static method that took an array of _A objects.

"Bob Rock" <no***************************@hotmail.com> wrote in message
news:u8**************@TK2MSFTNGP10.phx.gbl...
I suppose you want a static method (one that can be called using the
class name rather than a specific instance), but this has nothing in
particular to do with arrays.

What exactly are you trying to achieve?

P.

Static methods are available on classes.
This is what I need to do:
1) define a class.
2) create an array of objects of this class.
3) define methods on the array ..... NOT methods on a specific object in

the array (instance methods - MyArray[1].MyMethod()), not on the class (class or static methods - MyClass.MyMethod()) but on the array (MyArray.MyMethod()).
public class MyClass
{

}

public class StartupClass
{
public static main()
{
MyClass myArray = new MyClass[10];
myArray.MyMethod(); // this is what I'd like to have
}
}

What I'd like to do is be able to define MyMethod(). A method that is
available on the array, not on the class or on an element/object of the
array.
Hope this clarifies my intent.

Bob Rock

Jul 21 '05 #4

P: n/a
> This is not supported. The problem is you want to use an array of objects
as
the object instance in the method call, and only a single instance can be
specified. This instance is used as the object instance in the method body. In other words, if your class defined a field, one per instance, then each
object in the array would have its own copy of that field. In the method
itself, if there was a reference to the field then it would be impossible to determine which object instance you meant.

Instead, you could define a static method that took an array of _A objects.


David, I'm already using a static method that takes an array of my objects.
Still, I was wondering if there was some way of adding to the methods that
are available when using an array of my objects. I was thinking that this IS
an array, but IT IS an array of specific objects, so somehow it would be
possible to know what other methods should be made available on the array.

Anyhow I've seen that the methods available on any array are those defined
in the array abstract class and that these are implemented for each type in
the CLR. So somehow there must be a way of implementing the behavior of
these methods for an array of a custom class objects. I wonder just how.

Bob Rock
Jul 21 '05 #5

P: n/a
"Bob Rock" <no***************************@hotmail.com> wrote in message
news:uh**************@TK2MSFTNGP11.phx.gbl...
This is not supported. The problem is you want to use an array of objects
as
the object instance in the method call, and only a single instance can
be specified. This instance is used as the object instance in the method

body.
In other words, if your class defined a field, one per instance, then each object in the array would have its own copy of that field. In the method
itself, if there was a reference to the field then it would be

impossible to
determine which object instance you meant.

Instead, you could define a static method that took an array of _A objects.


David, I'm already using a static method that takes an array of my

objects. Still, I was wondering if there was some way of adding to the methods that
are available when using an array of my objects. I was thinking that this IS an array, but IT IS an array of specific objects, so somehow it would be
possible to know what other methods should be made available on the array.

Anyhow I've seen that the methods available on any array are those defined
in the array abstract class and that these are implemented for each type in the CLR. So somehow there must be a way of implementing the behavior of
these methods for an array of a custom class objects. I wonder just how.


You would have to define your own "array" class to do this.
--
John Saunders
johnwsaundersiii at hotmail
Jul 21 '05 #6

P: n/a
> You would have to define your own "array" class to do this.
--
John Saunders
johnwsaundersiii at hotmail


Yes, that is what I thought too having seen that methods available on any
array are those defined by the abstract class Array.
But I can't find any reference to point me in the right direction.

Bob Rock

Jul 21 '05 #7

P: n/a
"Bob Rock" <no***************************@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
You would have to define your own "array" class to do this.
--
John Saunders
johnwsaundersiii at hotmail


Yes, that is what I thought too having seen that methods available on any
array are those defined by the abstract class Array.
But I can't find any reference to point me in the right direction.


Check out the article "Walkthrough: Creating Your Own Collection Class" at
(http://msdn.microsoft.com/library/de...-us/vbcon/html
/vaconcreatingyourowncollectionclass.asp)
--
John Saunders
johnwsaundersiii at hotmail
Jul 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.