473,322 Members | 1,671 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,322 software developers and data experts.

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

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
7 1703
"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
> 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
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
> 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
"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
> 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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
4
by: ORi | last post by:
Hi all ! There's a question I've been bothering for a while: I'm actually developing architectural frameworks for application developing and I think virtual methods, although needed because of...
7
by: Bob Rock | last post by:
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...
5
by: Dale | last post by:
Is it possible to declare a method of an interface as static? For instance, can I create an interface that defines a static method and 2 instance methods?
4
by: Madhu | last post by:
Hi all, I am new to c#. I was trying out the following: class A { private static void doSomething() { System.Console.WriteLine("Class A is doing something"); }
5
by: Sadeq | last post by:
Enums are useful when we want to constrain the user to choose from a specified number of predifined valuse. But they only accept integral types as their underlying type. I encountered a case in...
3
by: rickeringill | last post by:
Hi comp.lang.javascript, I'm throwing this in for discussion. First up I don't claim to be any sort of authority on the ecmascript language spec - in fact I'm a relative newb to these more...
26
by: Cliff Williams | last post by:
Can someone explain the pros/cons of these different ways of creating a class? // 1 function myclass() { this.foo1 = function() {...} } // 2a
0
by: Peter Duniho | last post by:
On Mon, 01 Sep 2008 16:14:10 -0700, Blip <blip@krumpli.comwrote: Briefly, an anonymous method is exactly that: a method without a name. When you use the "delegate" keyword to declare an...
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
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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.