473,805 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.MyMetho d();

where MyMethod is something I defined (somehow).

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

Bob Rock


Jul 21 '05 #1
7 1731
"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.MyMetho d()) but on the array (MyArray.MyMeth od()).

public class MyClass
{

}

public class StartupClass
{
public static main()
{
MyClass myArray = new MyClass[10];
myArray.MyMetho d(); // 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******** ******@TK2MSFTN GP10.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.MyMetho d()) but on the array (MyArray.MyMeth od()).
public class MyClass
{

}

public class StartupClass
{
public static main()
{
MyClass myArray = new MyClass[10];
myArray.MyMetho d(); // 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******** ******@TK2MSFTN GP11.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
johnwsaundersii i at hotmail
Jul 21 '05 #6
> You would have to define your own "array" class to do this.
--
John Saunders
johnwsaundersii i 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******** ********@TK2MSF TNGP11.phx.gbl. ..
You would have to define your own "array" class to do this.
--
John Saunders
johnwsaundersii i 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 "Walkthroug h: Creating Your Own Collection Class" at
(http://msdn.microsoft.com/library/de...-us/vbcon/html
/vaconcreatingyo urowncollection class.asp)
--
John Saunders
johnwsaundersii i at hotmail
Jul 21 '05 #8

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

Similar topics

32
4533
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
4
1933
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 the flexibility they introduce (flexibility really needed in framework developing), are often a nuisance for final developers. They don't like them because they never know if base class must be called and where should they place the call if...
7
1043
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 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;
5
4347
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
1126
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
1647
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 which I have to constrain the use to choose from a specified number of predifined int arrays. For example: readonly int choice1 ={ 1, 2, 840, 113549, 1, 1, 2 }; readonly int choice2 ={ 1, 2, 840, 113549, 1, 1, 4 }; readonly int choice3 ={ 1, 2,...
3
1783
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 esoteric uses (abuses?) of the language. I've been working from the oft quoted resource http://www.crockford.com/javascript/private.html. During my first serious attempt at using the knowledge acquired from this page, I ran up against the problem...
26
2552
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
1423
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 anonymous method, all you're doing is writing a method the same as you would anywhere else, except that it doesn't have a name, and so you have to use it right away rather than being able to refer to it elsewhere. (And I mean that only in the static,...
0
9718
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10614
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10369
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10109
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5544
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3847
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.