IEnumerator IEnumerable vb 到 C#

IEnumerator IEnumerable vb to C#

大家好,我是 c# 语言的新手 vb.net, 在下面这段代码有什么错误以及原因,谢谢

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

但是当我尝试将此代码用于 c# 时出现错误

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;
    }
}

错误:属性 或索引器“Example.splitstring.current”无法分配给 – 它是只读的

您的当前和函数返回值变量未在您的代码中定义,请在函数中定义。

   Current = this.Sentence.Substring(this.currentPosition, tmpLength); // Error
   return functionReturnValue;

您尚未为 Current 属性 声明 set 块,因此它是只读的。你需要实现它,比如:

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;
    }
    set
    {
        this.Current = value;
    }
}

这个

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

是 "old" VB 在方法中设置 return 值而不使用 Return 关键字的方法。一般来说,下面VB代码

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

可以改写为

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

(只要some other code不退出方法)

属性也是如此。因此,我们首先将您的 old VB 代码重写为 new VB code:

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

现在 C# 的翻译应该很明显了。

(出于 VB6 向后 compatibility/Legacy 的原因)VB.NET 允许您通过设置从 PropertyFunction return 值Function/Property 名称到一个值。

来自文档:Function Procedures

"... The procedure returns this value in one of two ways:..."

  • "...It assigns a value to its own function name in one or more statements of the procedure. Control does not return to the calling program until an Exit Function or End Function statement is executed..."

例如

Public Function TestFunc() As String
    TestFunc = "bar"
    'some code
End Function

这大致相当于:

Public Function TestFunc() As String
    Dim temp = "bar"
    'some code
    Return temp
End Function

因此在您的 VB 代码中,它正在设置 属性 名称以便 return 一个值:

Public ReadOnly Property Test As String
    Get
        Test = "foo"
    End Get
End Property

或者您的情况:

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

C# 中没有与此等价的 direct,因为 return 会立即 return。最好 所以等效的 C# 将设置一个临时变量并且 return 即:

var temp= this.Sentence.Substring(this.currentPosition, tmpLength);
//some more code
return temp;

每当我从 VB 中的 class 中访问 属性 时,我总是在我的属性前加上 Me. 前缀,这避免了这种讨厌的行为