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

Void/function on a class which is an array

P: n/a
Hello.
Look at the code below.
I'm trying to create a function/void for when a class is declared as an
array.
Is it possible? If so, how?

Regards,
Oyvind Eriksen
public class Main
{
SomeClass[] array = SomeClass[10];
array.MyVoid(); <--- how do I create this void?
}
public class SomeClass
{

}
Oct 2 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
<"Oyvind Eriksen" <oyvind -(at)- eriksen.cn>wrote:
Look at the code below.
I'm trying to create a function/void for when a class is declared as an
array.
Is it possible? If so, how?
No, you can't do that. You could create a static method in SomeClass
which takes a SomeClass[] as a parameter though.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 2 '06 #2

P: n/a

The next release of C# (3.0) will include a new feature called
"extension methods" that will let you add new method to existing types.
However, I'm not sure if this will include array types, if not then
you could probably add your method to the Array base class. Either
way, this probably isn't an ideal solution, and you might be better off
passing your array as a parameter to another method.

Oyvind Eriksen (at) wrote:
Hello.
Look at the code below.
I'm trying to create a function/void for when a class is declared as an
array.
Is it possible? If so, how?

Regards,
Oyvind Eriksen
public class Main
{
SomeClass[] array = SomeClass[10];
array.MyVoid(); <--- how do I create this void?
}
public class SomeClass
{

}
Oct 2 '06 #3

P: n/a

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
<"Oyvind Eriksen" <oyvind -(at)- eriksen.cn>wrote:
>Look at the code below.
I'm trying to create a function/void for when a class is declared as an
array.
Is it possible? If so, how?

No, you can't do that. You could create a static method in SomeClass
which takes a SomeClass[] as a parameter though.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
I see. It would be practical though... :-)
But a static method with SomeClass[] would solve the problem.
Or create a collection?

Regards,
Oyvind Eriksen
Oct 2 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.