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

  • Thread starter Thread starter Bob Rock
  • Start date Start date
B

Bob Rock

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
 
Bob Rock said:
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.
 
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
 
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 said:
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
 
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
 
Bob Rock said:
objects impossible

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.
 
You would have to define your own "array" class to do this.
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
 
Back
Top