473,323 Members | 1,537 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,323 software developers and data experts.

What is the best way to make extended IEnumerator safe?

Hi,

I am extending standard IEnumerator, and I was just wondering what is the
best way to make enumarator safe? What do I mean by safe? Detect deletes and
all...
My idea is to have private Guid state field in the collection, and every
time something is inserted or deleted from the collection, I will just
change the guid. Enumerator will just have to compare the guid received in
the begging to the current one. If they are different, the collection has
changed.

What are your thought on this?

Thank you,
Sasha
Jul 21 '05 #1
9 1812

"Sasha" <no@no.com> wrote in message
news:Ok**************@TK2MSFTNGP12.phx.gbl...
Hi,

I am extending standard IEnumerator, and I was just wondering what is the
best way to make enumarator safe? What do I mean by safe? Detect deletes and all...
My idea is to have private Guid state field in the collection, and every
time something is inserted or deleted from the collection, I will just
change the guid. Enumerator will just have to compare the guid received in
the begging to the current one. If they are different, the collection has
changed.

That would work, but would probably be overkill. It would be simple enough
just to maintain an Int32 or Int64 version counter and increment it every
time the collection changes, store the value when the IEnumerator is created
and check against it. It would probably be faster and less troublesome, just
a version++; \ version = version+1 to update(depending on your language, not
sure if vb.net has an increment operator...). Its not likely your collection
is going to change enough to overflow an Int32, let alone an Int64.
What are your thought on this?

Thank you,
Sasha

Jul 21 '05 #2
How would I pass it from the collection to the Enumerator?

I can expose it as a readonly property, but it is not very elegant...
I wish .Net had "friend" keyword, just like C++....

"Daniel O'Connell" <onyxkirx@--NOSPAM--comcast.net> wrote in message
news:ziJcb.581241$o%2.261385@sccrnsc02...

"Sasha" <no@no.com> wrote in message
news:Ok**************@TK2MSFTNGP12.phx.gbl...
Hi,

I am extending standard IEnumerator, and I was just wondering what is the best way to make enumarator safe? What do I mean by safe? Detect deletes and
all...
My idea is to have private Guid state field in the collection, and every
time something is inserted or deleted from the collection, I will just
change the guid. Enumerator will just have to compare the guid received in the begging to the current one. If they are different, the collection has changed.


That would work, but would probably be overkill. It would be simple enough
just to maintain an Int32 or Int64 version counter and increment it every
time the collection changes, store the value when the IEnumerator is

created and check against it. It would probably be faster and less troublesome, just a version++; \ version = version+1 to update(depending on your language, not sure if vb.net has an increment operator...). Its not likely your collection is going to change enough to overflow an Int32, let alone an Int64.
What are your thought on this?

Thank you,
Sasha


Jul 21 '05 #3

"Sasha" <no@no.com> wrote in message
news:e1**************@TK2MSFTNGP10.phx.gbl...
How would I pass it from the collection to the Enumerator?
Simpilest method would be a readonly property, but that is only elegent if
other classes could have a use for the version.

Have you considered an inner class? I apologize for posting this in C# code,
I don't konw enough VB to whip this up readily, as I'm not sure about inner
class syntax and the like. This code will also not compile, it is really
just a sample showing the basis of what i mean.

public class MyClass : IEnumerable
{

public IEnumerator GetEnumerator() {

}
int version;
//define a class internally to yoru colelction that implements IEnumerator
class MyClassEnumerator : IEnumerator
{
public myClassEnumerator(MyClass class) {
//should let you access private members of MyClass
baseVersion = class.version;
this.class = class;
}
MyClass class;
int baseVersion;
public object Current {
get {
if (class.Version != baseVersion)
//throw your exception;
return <getobject>;
}
}
}
}
I can expose it as a readonly property, but it is not very elegant...
I wish .Net had "friend" keyword, just like C++....

"Daniel O'Connell" <onyxkirx@--NOSPAM--comcast.net> wrote in message
news:ziJcb.581241$o%2.261385@sccrnsc02...

"Sasha" <no@no.com> wrote in message
news:Ok**************@TK2MSFTNGP12.phx.gbl...
Hi,

I am extending standard IEnumerator, and I was just wondering what is the best way to make enumarator safe? What do I mean by safe? Detect deletes
and
all...
My idea is to have private Guid state field in the collection, and
every time something is inserted or deleted from the collection, I will just
change the guid. Enumerator will just have to compare the guid
received
in the begging to the current one. If they are different, the collection has changed.


That would work, but would probably be overkill. It would be simple

enough just to maintain an Int32 or Int64 version counter and increment it every time the collection changes, store the value when the IEnumerator is

created
and check against it. It would probably be faster and less troublesome,

just
a version++; \ version = version+1 to update(depending on your language,

not
sure if vb.net has an increment operator...). Its not likely your

collection
is going to change enough to overflow an Int32, let alone an Int64.
What are your thought on this?

Thank you,
Sasha



Jul 21 '05 #4
vb.net DOES, in fact, support the friend keyword.
"Sasha" <no@no.com> wrote in message
news:e1**************@TK2MSFTNGP10.phx.gbl...
How would I pass it from the collection to the Enumerator?

I can expose it as a readonly property, but it is not very elegant...
I wish .Net had "friend" keyword, just like C++....

"Daniel O'Connell" <onyxkirx@--NOSPAM--comcast.net> wrote in message
news:ziJcb.581241$o%2.261385@sccrnsc02...

"Sasha" <no@no.com> wrote in message
news:Ok**************@TK2MSFTNGP12.phx.gbl...
Hi,

I am extending standard IEnumerator, and I was just wondering what is the best way to make enumarator safe? What do I mean by safe? Detect deletes
and
all...
My idea is to have private Guid state field in the collection, and
every time something is inserted or deleted from the collection, I will just
change the guid. Enumerator will just have to compare the guid
received
in the begging to the current one. If they are different, the collection has changed.


That would work, but would probably be overkill. It would be simple

enough just to maintain an Int32 or Int64 version counter and increment it every time the collection changes, store the value when the IEnumerator is

created
and check against it. It would probably be faster and less troublesome,

just
a version++; \ version = version+1 to update(depending on your language,

not
sure if vb.net has an increment operator...). Its not likely your

collection
is going to change enough to overflow an Int32, let alone an Int64.
What are your thought on this?

Thank you,
Sasha



Jul 21 '05 #5
Oh, This is great! Very nice and elegant solution!

Thank you very much!
"Daniel O'Connell" <onyxkirx@--NOSPAM--comcast.net> wrote in message
news:2vKcb.578879$Ho3.107609@sccrnsc03...

"Sasha" <no@no.com> wrote in message
news:e1**************@TK2MSFTNGP10.phx.gbl...
How would I pass it from the collection to the Enumerator?
Simpilest method would be a readonly property, but that is only elegent if
other classes could have a use for the version.

Have you considered an inner class? I apologize for posting this in C#

code, I don't konw enough VB to whip this up readily, as I'm not sure about inner class syntax and the like. This code will also not compile, it is really
just a sample showing the basis of what i mean.

public class MyClass : IEnumerable
{

public IEnumerator GetEnumerator() {

}
int version;
//define a class internally to yoru colelction that implements IEnumerator
class MyClassEnumerator : IEnumerator
{
public myClassEnumerator(MyClass class) {
//should let you access private members of MyClass
baseVersion = class.version;
this.class = class;
}
MyClass class;
int baseVersion;
public object Current {
get {
if (class.Version != baseVersion)
//throw your exception;
return <getobject>;
}
}
}
}
I can expose it as a readonly property, but it is not very elegant...
I wish .Net had "friend" keyword, just like C++....

"Daniel O'Connell" <onyxkirx@--NOSPAM--comcast.net> wrote in message
news:ziJcb.581241$o%2.261385@sccrnsc02...

"Sasha" <no@no.com> wrote in message
news:Ok**************@TK2MSFTNGP12.phx.gbl...
> Hi,
>
> I am extending standard IEnumerator, and I was just wondering what is
the
> best way to make enumarator safe? What do I mean by safe? Detect deletes and
> all...
> My idea is to have private Guid state field in the collection, and every > time something is inserted or deleted from the collection, I will
just > change the guid. Enumerator will just have to compare the guid received
in
> the begging to the current one. If they are different, the

collection has
> changed.
>

That would work, but would probably be overkill. It would be simple enough just to maintain an Int32 or Int64 version counter and increment it every time the collection changes, store the value when the IEnumerator is

created
and check against it. It would probably be faster and less

troublesome, just
a version++; \ version = version+1 to update(depending on your
language, not
sure if vb.net has an increment operator...). Its not likely your

collection
is going to change enough to overflow an Int32, let alone an Int64.

> What are your thought on this?
>
> Thank you,
> Sasha
>
>



Jul 21 '05 #6
Steve,
However VB.NET Friend <> C++ Friend!

In VB.NET Friend means that the property is visible to every one in the
assembly.

In C++ Friend means that just this one specific class is my friend, making
it significantly more intimate.

Notice that Sasha stated "had "friend" keyword, just like C++...."

Hope this helps
Jay

"steve" <as*@abc.com> wrote in message
news:vn************@corp.supernews.com...
vb.net DOES, in fact, support the friend keyword.
"Sasha" <no@no.com> wrote in message
news:e1**************@TK2MSFTNGP10.phx.gbl...
How would I pass it from the collection to the Enumerator?

I can expose it as a readonly property, but it is not very elegant...
I wish .Net had "friend" keyword, just like C++....

"Daniel O'Connell" <onyxkirx@--NOSPAM--comcast.net> wrote in message
news:ziJcb.581241$o%2.261385@sccrnsc02...

"Sasha" <no@no.com> wrote in message
news:Ok**************@TK2MSFTNGP12.phx.gbl...
> Hi,
>
> I am extending standard IEnumerator, and I was just wondering what is
the
> best way to make enumarator safe? What do I mean by safe? Detect deletes and
> all...
> My idea is to have private Guid state field in the collection, and every > time something is inserted or deleted from the collection, I will
just > change the guid. Enumerator will just have to compare the guid received
in
> the begging to the current one. If they are different, the

collection has
> changed.
>

That would work, but would probably be overkill. It would be simple enough just to maintain an Int32 or Int64 version counter and increment it every time the collection changes, store the value when the IEnumerator is

created
and check against it. It would probably be faster and less

troublesome, just
a version++; \ version = version+1 to update(depending on your
language, not
sure if vb.net has an increment operator...). Its not likely your

collection
is going to change enough to overflow an Int32, let alone an Int64.

> What are your thought on this?
>
> Thank you,
> Sasha
>
>



Jul 21 '05 #7
Hi Sasha,

You want a safe IEnumerator, which makes sense - sort of. I'm curious as
to what you want this for.

If the Enumerator detects that a collection has been changed, it could be
due to a bug in the code (the Collection isn't supposed to be changed), or due
to external 'interference' (eg another thread). In these cases I guess you'd
want an Exception.

It could be intended behaviour, eg deleting certain items from the
Collection. In this case you could argue that ISafeEnumerator shouldn't be
used. But what if I wanted it to gracefully deal with this and 'skip' the
deletions? Is this part of the plan?

Basically - What's it all about? ;-)

Curiously,
Fergus
Jul 21 '05 #8
Public Class [MyClass]
Implements IEnumerable 'ToDo: Add Implements Clauses for implementation
methods of these interface(s)
Public Function GetEnumerator() As IEnumerator
End Function 'GetEnumerator
Private version As Integer
_
'define a class internally to yoru colelction that implements IEnumerator
Class MyClassEnumerator
Implements IEnumerator 'ToDo: Add Implements Clauses for
implementation methods of these interface(s)

Public Sub New(__unknown As [MyClass])
If (True) Then '
'ToDo: Error processing original source shown below
'
'
'--------------------------------------^--- Syntax error:
'identifier' expected
'
'ToDo: Error processing original source shown below
'
'
'-------------------------------------------^--- expression
expected
'should let you access private members of MyClass
baseVersion = __unknown
__unknown.version '
'ToDo: Error processing original source shown below
'
'
'-------------------^--- expression expected
'
'ToDo: Error processing original source shown below
'
'
'------------------------^--- expression expected
Me.__unknown
__unknown = __unknown '
'ToDo: Error processing original source shown below
'
'
'----------^--- Syntax error: 'identifier' expected
'
'ToDo: Error processing original source shown below
'
'
'----------------^--- expression expected
'
'ToDo: Error processing original source shown below
'
'
'------------------^--- expression expected
End If
'
'ToDo: Error processing original source shown below
'
'
'---------^--- Syntax error: ';' expected
Dim baseVersion As Integer
Dim Current As Object
If (True) Then '
'ToDo: Error processing original source shown below
'
'
'-^--- expression expected
'
'ToDo: Error processing original source shown below
'
'
'-----------------------^--- Syntax error: ';' expected
If (True) Then '
'ToDo: Error processing original source shown below
'
'
'---------^--- Syntax error: ';' expected
If __unknown Then '
'ToDo: Error processing original source shown below
'
'
'-------------^--- expression expected
'
'ToDo: Error processing original source shown below
'
'
'------------------^--- expression expected
'
'ToDo: Error processing original source shown below
'
'
'-----------------------------------------^--- Syntax error:
';' expected
End If
__unknown.Version <> baseVersion
'throw your exception;
Return __unknown < getobject > __unknown '
'ToDo: Error processing original source shown below
'
'
'----------------^--- expression expected
'
'ToDo: Error processing original source shown below
'
'
'---------------------------^--- expression expected
'
'ToDo: Error processing original source shown below
'System.Char[]
'^--- Syntax error: '}' expected
End If
End If
End Sub 'New
End Class 'MyClassEnumerator
End Class '[MyClass]
Jul 21 '05 #9
That's exactly what I meant by safe Enumerator. If the collection changes,
I should throw an exception. Just like it happens in standard enumerators.

I just wanted to know the best way to track the state of the collection. I
could have checked the count every time I do MoveNext, but this is a slight
probability that one item was inserted and another one was removed.

So anyways, you are right about this. And that is what I meant by "safe"
enumerator. If a collection changes during enumeration, I need throw an
exception.

Thank you very much for your anwer.

Take Care,
Sasha
"Fergus Cooney" <fi******@tesco.net> wrote in message
news:ul****************@TK2MSFTNGP12.phx.gbl...
Hi Sasha,

You want a safe IEnumerator, which makes sense - sort of. I'm curious as to what you want this for.

If the Enumerator detects that a collection has been changed, it could be due to a bug in the code (the Collection isn't supposed to be changed), or due to external 'interference' (eg another thread). In these cases I guess you'd want an Exception.

It could be intended behaviour, eg deleting certain items from the
Collection. In this case you could argue that ISafeEnumerator shouldn't be
used. But what if I wanted it to gracefully deal with this and 'skip' the
deletions? Is this part of the plan?

Basically - What's it all about? ;-)

Curiously,
Fergus

Jul 21 '05 #10

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

Similar topics

9
by: Matthias Kaeppler | last post by:
Hi, I'm using phpWebSite to build my website and I want it to be standard conforming. However, the CSS validator always outputs an error: "Please, validate your XML document first! The...
9
by: Sasha | last post by:
Hi, I am extending standard IEnumerator, and I was just wondering what is the best way to make enumarator safe? What do I mean by safe? Detect deletes and all... My idea is to have private Guid...
1
by: midnight madness | last post by:
I tried but failed to implement a template class that support IEnumerator<T> interface using C++/CLI in VS 2005 Professional version. I could not figure out the proper syntax to implement the...
11
by: Leslie Sanford | last post by:
I've been kicking around an idea mostly as a thought experiment at this point. The idea is to create a thread safe collection that implements the IEnumerable interface. The enumerators it creates...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.