473,385 Members | 1,907 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,385 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


Nov 16 '05 #1
7 1032
"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.
Nov 16 '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
Nov 16 '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

Nov 16 '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
Nov 16 '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
Nov 16 '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

Nov 16 '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
Nov 16 '05 #8

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

Similar topics

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...
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...
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
2
by: =?Utf-8?B?Z2FkeWE=?= | last post by:
I use one of 2 arrays dependent on the country. Rather than say: if exchangeID = 1 then dim myPlaceBets() as As UK.exchange.PlaceBets many statements myPlaceBetsReq.bets = myPlaceBets else...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.