2016-04-29 67 views
5

大家好我是新來的C#語言,我是用vb.net, 下面這有什麼代碼,爲什麼錯誤,謝謝的IEnumerator IEnumerable的VB到C#

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」不能已經分配到 - 它是隻讀

+1

嘗試用'var functionReturnValue = this.Sentence.Substring'替換'Current = this.Sentence.Substring ...'# –

+0

@YacoubMassad感謝ycoub先生,但爲什麼我不得不寫「function Return Value」我想要準確理解爲什麼發生此錯誤 –

+1

有趣。我想這與VB6的向後兼容性有關。在那裏你可以給方法名分配一個值,而不是返回它。所以這隻適用於只讀屬性getter,無處可用。 –

回答

5

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

是設定的方法的返回值不使用Return關鍵字的「老」 VB的方式。一般來說,下面的VB代碼

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

可以(只要some other code不退出方法)改寫爲

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

屬性也是如此。因此,我們首先重寫 VB代碼 VB代碼:

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

,現在翻譯到C#應該是顯而易見的。

+0

什麼是正確的代碼與C# –

0

當前和functionReturnValue瓦里能夠在你的代碼中定義函數中沒有定義。

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

您還沒有宣佈參加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; 
    } 
} 
+0

是的,我沒有爲Current聲明一個設置塊,因爲當你實現接口IEnumerator時,Visual Studio會生成沒有設置的當前屬性塊 公共對象當前 {throw new NotImplementedException(); } } –

3

(對於VB6向後兼容性/傳統的原因)VB.NET允許從一個或PropertyFunctionFunction/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代碼

因此,它是設置屬性名稱,以返回值:

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

或在您的情況:

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

Ther e是直接相當於C#中的這個,因爲return會立即返回。最好的所以相當於C#是設置一個臨時變量,並返回:

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

每當我從VB類中的訪問屬性,我一直前綴我的屬性與Me.避免了這個討厭的行爲