IEnumerator IEnumerable vb to C#

Nour Ahmed

Hello everyone i'm new to c# language i was use vb.net, in below what is the error with this code and why, thank you

vb.net code
Class SplitString
Implements IEnumerable
Implements IEnumerator

Private currentPosition As Integer = 0
Private m_Sentence As String
Property Sentence() As String
    Get
        Return m_Sentence
    End Get
    Set(ByVal Value As String)
        m_Sentence = Value
        Me.Reset()
    End Set
End Property

Public ReadOnly Property Current As Object Implements IEnumerator.Current
    Get
        Dim counter As Integer
        Dim tmpLength As Integer = 0
        For counter = Me.currentPosition To Me.Sentence.Length - 1
            If Me.Sentence.Chars(counter) = " "c Then
                Exit For
            Else
                tmpLength += 1
            End If
        Next
        Current = Me.Sentence.Substring(Me.currentPosition, tmpLength) ' ok 
        Me.currentPosition += tmpLength + 1
    End Get
End Property

Public Function MoveNext() As Boolean Implements IEnumerator.MoveNext
    If Me.currentPosition > Me.Sentence.Length - 1 Then
        Me.Reset()
        Return False
    Else
        Return True
    End If
End Function

Public Sub Reset() Implements IEnumerator.Reset
    Me.currentPosition = 0
End Sub

Public Function GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
    Return Me
End Function
End Class

but when i try this code to c# i get Error

c# code
class SplitString:IEnumerable,IEnumerator
{
    private int currentPosition = 0;
    private string m_Sentence;
    public string Sentence
    {
        get { return m_Sentence; }
        set
        {
            m_Sentence = value;
            this.Reset();
        }
    }
    public IEnumerator GetEnumerator()
    {
        return this;
    }


    public object Current
    {
        get
        {
            int counter = 0;
            int tmpLength = 0;
            for (counter = this.currentPosition; counter <= this.Sentence.Length - 1; counter++)
            {
                if (this.Sentence[counter] == ' ')
                {
                    break; 
                }
                else
                {
                    tmpLength += 1;
                }
            }
            Current = this.Sentence.Substring(this.currentPosition, tmpLength); // Error
            this.currentPosition += tmpLength + 1;
            return functionReturnValue;
        }
    }
    public bool MoveNext()
    {
       if (this.currentPosition > this.Sentence.Length-1)
       {
           this.Reset();
           return false;
       }
       else
       {
           return true;
       }
    }

    public void Reset()
    {
        this.currentPosition=0;
    }
}

error: Property or indexer ‘Example.splitstring.current’ cannot ve assigned to – it is read only

Heinzi

This

Current = Me.Sentence.Substring(Me.currentPosition, tmpLength) ' ok 

is the "old" VB way of setting a return value in a method without using the Return keyword. In general, the following VB code

    myMethodName = ...
    ...some other code...
End Function

can be rewritten as

    Dim someTempVariable = ...
    ...some other code...
    Return someTempVariable
End Function

(as long as some other code does not exit the method).

The same is true for properties. Thus, we first rewrite your old VB code to new VB code:

        ...
    Next
    Dim returnValue = Me.Sentence.Substring(Me.currentPosition, tmpLength) 
    Me.currentPosition += tmpLength + 1
    Return returnValue
End Get

and now the translation to C# should be obvious.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why doesn't the following work? (IEnumerable/ IEnumerator)

From Dev

VB.NET sort IEnumerable class

From Dev

understanding IEnumerable<T> in C#

From Dev

Implementing IEnumerator/ IEnumerable with IDispose error

From Dev

How does Visual Studio evaluate the IEnumerable without breaking into its IEnumerator<T>'s MoveNext?

From Dev

What is the reason implementing IEnumerable and IEnumerator

From Dev

Why is IEnumerable<T> necessary when there is IEnumerator<T>?

From Dev

IEnumerable<T> and IEnumerator - some clarification please

From Dev

Concatenation of IEnumerable collections C#

From Dev

C# IEnumerable<string> and string[]

From Dev

When using "yield" why does compiler-generated type implement both IEnumerable and IEnumerator

From Dev

C#: Custom IEnumerator?

From Dev

Instantiating an IEnumerable array in C#

From Dev

extending ienumerable method C#

From Dev

C#: Get distinct T from IEnumerable<IEnumerable<T>>

From Dev

How does Visual Studio evaluate the IEnumerable without breaking into its IEnumerator<T>'s MoveNext?

From Dev

Add Item to a IEnumerable populated using Linq (vb.net)

From Dev

IEnumerable<T> and IEnumerator - some clarification please

From Dev

How to return from ienumerable function in vb.net

From Dev

C# IEnumerator with foreach loops, best design pattern?

From Dev

Transforming an IEnumerable C#

From Dev

C# IEnumerator is unchangable

From Dev

c# check IEnumerable for null

From Dev

How to return IEnumerable datatable in VB.NET

From Dev

C# IEnumerable and IList difference

From Dev

COM - use IEnumerable in C++

From Dev

Join two lists returns IEnumerable in IEnumerable C# Entity framework

From Dev

Cast IEnumerable<string> to IEnumerable<int> in C#

From Dev

IEnumerator does not implement interface IEnumerable

Related Related

  1. 1

    Why doesn't the following work? (IEnumerable/ IEnumerator)

  2. 2

    VB.NET sort IEnumerable class

  3. 3

    understanding IEnumerable<T> in C#

  4. 4

    Implementing IEnumerator/ IEnumerable with IDispose error

  5. 5

    How does Visual Studio evaluate the IEnumerable without breaking into its IEnumerator<T>'s MoveNext?

  6. 6

    What is the reason implementing IEnumerable and IEnumerator

  7. 7

    Why is IEnumerable<T> necessary when there is IEnumerator<T>?

  8. 8

    IEnumerable<T> and IEnumerator - some clarification please

  9. 9

    Concatenation of IEnumerable collections C#

  10. 10

    C# IEnumerable<string> and string[]

  11. 11

    When using "yield" why does compiler-generated type implement both IEnumerable and IEnumerator

  12. 12

    C#: Custom IEnumerator?

  13. 13

    Instantiating an IEnumerable array in C#

  14. 14

    extending ienumerable method C#

  15. 15

    C#: Get distinct T from IEnumerable<IEnumerable<T>>

  16. 16

    How does Visual Studio evaluate the IEnumerable without breaking into its IEnumerator<T>'s MoveNext?

  17. 17

    Add Item to a IEnumerable populated using Linq (vb.net)

  18. 18

    IEnumerable<T> and IEnumerator - some clarification please

  19. 19

    How to return from ienumerable function in vb.net

  20. 20

    C# IEnumerator with foreach loops, best design pattern?

  21. 21

    Transforming an IEnumerable C#

  22. 22

    C# IEnumerator is unchangable

  23. 23

    c# check IEnumerable for null

  24. 24

    How to return IEnumerable datatable in VB.NET

  25. 25

    C# IEnumerable and IList difference

  26. 26

    COM - use IEnumerable in C++

  27. 27

    Join two lists returns IEnumerable in IEnumerable C# Entity framework

  28. 28

    Cast IEnumerable<string> to IEnumerable<int> in C#

  29. 29

    IEnumerator does not implement interface IEnumerable

HotTag

Archive