Removing entries from AAs

useo6 unknown at unknown.com
Fri Aug 19 05:01:47 PDT 2011


Hi,

I've create a little example of my problem:

module example;

class ExampleClass {

	public {

		int mi;

		this(int i) {
			mi = i;
		}

	}

}

int main(string[] args) {

	ExampleClass[hash_t] exp;

	ExampleClass cex = new ExampleClass(1);
	exp[cex.toHash()] = cex;

	cex = new ExampleClass(2);
	exp[cex.toHash()] = cex;

	cex = new ExampleClass(3);
	exp[cex.toHash()] = cex;

	foreach (ExampleClass c; exp) {
		if (c.mi == 2) {exp.remove(c.toHash()); }
	}

	return 0;

}

When I run this small example-app, I get an "Access Violation". When
I insert a break into my foreach-loop like:

	foreach (ExampleClass c; exp) {
		if (c.mi == 2) {exp.remove(c.toHash()); break; }
	}

it works, but this means, I leave the foreach-loop. What I'm doing
wrong by using the foreach-block?

Thanks for any help!


More information about the Digitalmars-d-learn mailing list