473,472 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Iterators and static class

Hello all.
I have to implement IEnumerator interface in my (static) class. But
compilers throws me an error:
'GetEnumerator': cannot declare instance members in a static class
For example:

private static List<Product_productsList;
public static List<ProductProductsList
{
get
{
if (_productsList == null)
_productsList = new List<Product>();
return _productsList;
}
set { _productsList = value; }
}

public IEnumerator GetEnumerator()
{
foreach (Product p in _productsList)
{
yield return p;
}
}
That's why I have question: Can I implement Iterator with static
classes other way?
Thanks in advance

Oct 18 '06 #1
8 3392
dtarczynski wrote:

<snip>
That's why I have question: Can I implement Iterator with static
classes other way?
No. You can't implement any non-empty interface in a static class, as
an interface *insists* on certain instance members being present, and a
static class *prevents* any instance members being present.

Jon

Oct 18 '06 #2
Not really. The problem is that the foreach statement expects an instance
variable and not a type name. So, this code is illegal:

using System;
using System.Collections;
using System.Collections.Generic;

namespace ConsoleApp
{
static class EnumeratorTest
{
private static List<intm_IntList = new List<int>(new int[] { 0, 1,
2, 3, 4, 5, 6, 7, 8, 9 });

public static IEnumerator GetEnumerator()
{
foreach (int i in m_IntList)
yield return i;
}
}

class Program
{
static void Main(string[] args)
{
foreach (int i in EnumeratorTest)
Console.WriteLine(i);
}
}
}

This raises the following compiler error: "'ConsoleApp.EnumeratorTest' is
a 'type' but is used like a 'variable'"

However, you *can* add a ForEach method similar to the way it is done by
List<T>.ForEach(). For example:

using System;
using System.Collections;
using System.Collections.Generic;

namespace ConsoleApp
{
static class EnumeratorTest
{
private static List<intm_IntList = new List<int>(new int[] { 0, 1,
2, 3, 4, 5, 6, 7, 8, 9 });

public static void ForEach(Action<intaction)
{
foreach (int i in m_IntList)
action(i);
}
}

class Program
{
static void Main(string[] args)
{
EnumeratorTest.ForEach(delegate(int i) { Console.WriteLine(i); });
}
}
}
Best Regards,
Dustin Campbell
Developer Express Inc.
Oct 18 '06 #3
No. You can't implement any non-empty interface in a static class, as
an interface *insists* on certain instance members being present, and
a static class *prevents* any instance members being present.
You don't have to implement an interface to create a GetEnumerator() method
that uses an iterator. This code works fine:

using System;
using System.Collections;
using System.Collections.Generic;

namespace ConsoleApp
{
class EnumeratorTest
{
private List<intm_IntList = new List<int>(new int[] { 0, 1, 2, 3, 4,
5, 6, 7, 8, 9 });

public IEnumerator GetEnumerator()
{
foreach (int i in m_IntList)
yield return i;
}
}

class Program
{
static void Main(string[] args)
{
EnumeratorTest instance = new EnumeratorTest();
foreach (int i in instance)
Console.WriteLine(i);
}
}
}

Best Regards,
Dustin Campbell
Developer Express Inc.
Oct 18 '06 #4
There's a trick with static functions that return enumerators you can
use.

static class myStaticClass
{
static private object[] myIterable;

public static IEnumerable<objectMyStaticEnumerable() // a method
that returns an enumerable
{
foreach(object obj in myIterable)
{
yield return obj;
}
}
}

this can be used as follows:

foreach (obj in myStaticClass.MyStaticEnumerable())
{
...
}

I'm pretty sure that mess'll work, though I haven't tested it myself.

dtarczynski wrote:
Hello all.
I have to implement IEnumerator interface in my (static) class. But
compilers throws me an error:
'GetEnumerator': cannot declare instance members in a static class
For example:

private static List<Product_productsList;
public static List<ProductProductsList
{
get
{
if (_productsList == null)
_productsList = new List<Product>();
return _productsList;
}
set { _productsList = value; }
}

public IEnumerator GetEnumerator()
{
foreach (Product p in _productsList)
{
yield return p;
}
}
That's why I have question: Can I implement Iterator with static
classes other way?
Thanks in advance
Oct 18 '06 #5
Dustin Campbell <du*****@no-spam-pleasedevexpress.comwrote:
No. You can't implement any non-empty interface in a static class, as
an interface *insists* on certain instance members being present, and
a static class *prevents* any instance members being present.

You don't have to implement an interface to create a GetEnumerator() method
that uses an iterator.
That's true. You do, however, have to have an instance method - which
runs into exactly the same issue.

--
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 18 '06 #6
That's true. You do, however, have to have an instance method - which
runs into exactly the same issue.
That is the problem. Of course, this begs the question why a static class
is needed in the first place. It seems counterintuitive for a collection
class to be static since you can't make it enumerable and you can't have
static indexers.

Best Regards,
Dustin Campbell
Developer Express Inc.
Oct 18 '06 #7
Either way, if static-like behaviour is needed, C# has easy idioms for
Singletons. That is, define an instance class, but make the
constructor private - and then give the class itself a static
constructor which in turn constructs the instance object and stores it
in a public static field. Alternately, one can use a property and make
it lazily instantiated, rather than using the static constructor to
construct the single instance object.

Still, I prefer just using a generator to include a method that returns
an IEnumerable if static semantics are desired.

Dustin Campbell wrote:
That's true. You do, however, have to have an instance method - which
runs into exactly the same issue.

That is the problem. Of course, this begs the question why a static class
is needed in the first place. It seems counterintuitive for a collection
class to be static since you can't make it enumerable and you can't have
static indexers.

Best Regards,
Dustin Campbell
Developer Express Inc.
Oct 18 '06 #8
Either way, if static-like behaviour is needed, C# has easy idioms for
Singletons. That is, define an instance class, but make the
constructor private - and then give the class itself a static
constructor which in turn constructs the instance object and stores it
in a public static field. Alternately, one can use a property and
make it lazily instantiated, rather than using the static constructor
to construct the single instance object.

Still, I prefer just using a generator to include a method that
returns an IEnumerable if static semantics are desired.
Sure, or even the old System.Collections.ICollection interface.

Best Regards,
Dustin Campbell
Developer Express Inc.
Oct 18 '06 #9

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

Similar topics

0
by: Gregory Bond | last post by:
I'm trying to extend Python with an iterator class that should be returned from a factory function. For some reason the iterator object is not being properly initialised if the iterator is...
3
by: Alexander Stippler | last post by:
Hi, I have to design some two dimensional iterators and I'm not quite sure about the design. I'd like to have the iterators mostly STL-like. The STL does not contain two dimensional iterators, I...
9
by: richard.forrest1 | last post by:
I have a problem with an abstract interface class whose implementation classes need to return different iterator types (but with the same value_types etc). Classes A and B both conform to the...
2
by: Alvin | last post by:
Will it be possible to have more than one Iterator in a type?
1
by: Kamen Yotov | last post by:
Hello, I got my hands on the PDC preview version of whidbey and I think I managed to break the compiler/runtime with my first Generics/Iterators attempt. The reason I am posting it here is that I...
14
by: Jiri Kripac | last post by:
Languages such as Simula 67 contain a general concept of coroutines that allow the execution of a method to be suspended without rolling back the stack and then later resumed at the same place as...
6
by: Andrew Matthews | last post by:
Hi All, I have the following little class of iterators that allow me to iterate over elements in the file system. I have nested some of them, and then added Func<FileInfo, booldelegates to filter...
6
by: gexarchakos | last post by:
Hi there, Please give me at least a hint... I have a problem implementing a function object with parameters two iterators. That is: A class 'node' produces messages using a routing policy....
3
by: Jess | last post by:
Hello, Iterators are typically put into five different categories, namely input iterator, output iterator, forward iterator, bidirectional iterator and random iterator. The differences come...
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
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
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,...
0
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...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.