IEnumerator IEnumerable vb到C#

时间:2016-04-29 09:44:22

标签: c# .net vb.net vb.net-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'无法分配给 - 它是只读的

4 个答案:

答案 0 :(得分:5)

Return

是&#34; old&#34; VB方法在不使用 myMethodName = ... ...some other code... End Function 关键字的情况下在方法中设置返回值。一般来说,以下VB代码

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

可以改写为

some other code

(只要 ... Next Dim returnValue = Me.Sentence.Substring(Me.currentPosition, tmpLength) Me.currentPosition += tmpLength + 1 Return returnValue End Get 没有退出方法)。

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

{{1}}

现在C#的翻译应该是显而易见的。

答案 1 :(得分:3)

(对于VB6向后兼容性/遗留原因)VB.NET允许您通过将Property / Function名称设置为FunctionProperty来返回值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 值。

来自文档:Function Procedures

  

&#34; ...该过程以两种方式之一返回此值:...&#34;

     
      
  • &#34; ...它在一个或多个中为自己的函数名赋值   程序陈述。控制不会返回到调用   程序,直到退出函数或结束函数语句   执行...&#34;
  •   

e.g。

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

这大致相当于:

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

因此,在您的VB代码中,它设置属性名称以返回值:

return

或在你的情况下:

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

在C#中没有与直接相当的,因为Me.会立即返回。最好所以等效的C#将设置一个临时变量并返回:

imgUrl = "https://graph.facebook.com/" + user_id + "/picture?type=large";

每当我从VB中的类中访问一个Property时,我总是在$('#search-box').keyup(function() { // bind the search data var input = $('.search-input').val(); if (input.length >= 3) { $.getJSON({ // get JSON data url: 'example.com?keyword=' + input, success: function(data) { // do processing. var output = "<ul class='search-lists'>"; //output search data list $.each(data, function(key, val) { output += '<li><a>' + val.term + '</a></li>'; }); output += '</ul>'; $('.search-results').html(output); } }); // JSON request } }); // data bind 前面加上我的属性,这可以避免这种讨厌的行为

答案 2 :(得分:0)

您的Current和functionReturnValue变量未定义在函数中的代码中。

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

答案 3 :(得分:0)

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

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