2016-04-29 14 views
5

Ciao a tutti Sono nuovo di linguaggio C# ero uso vb.net, in seguito quello che è l'errore con questo codice e perché, grazieIEnumerator IEnumerable VB a 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 

ma quando provo questo codice in C# ottengo l'errore

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

errore: del proprietario o indicizzatore 'Example.splitstring.current' non può ve assegnata - si legge solo

+1

Provare a sostituire 'Current = this.Sentence.Substring ...' con 'var functionReturnValue = this.Sentence.Substring ...' –

+0

@YacoubMassad grazie a mr yacoub ma Perché sono costretto a scrivere "function Return Value" Voglio per capire esattamente perché si è verificato questo errore –

+1

Interessante. Immagino che abbia a che fare con la compatibilità all'indietro VB6. Qui puoi assegnare un valore al metodo-name anziché restituirlo. Quindi questo funziona solo nella proprietà di sola lettura, in nessun altro luogo. –

risposta

5

Questo

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

è il "vecchio" modo VB di impostazione di un valore di ritorno in un metodo senza utilizzare la parola chiave Return. In generale, il seguente codice VB

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

può essere riscritta come

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

(purché some other code non uscire dal metodo).

Lo stesso vale per le proprietà. Quindi, per prima cosa riscrivere il vecchio codice VB per nuovo codice VB:

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

e ora la traduzione di C# dovrebbe essere ovvio.

+0

qual è il codice corretto con C# –

0

il tuo attuale e functionReturnValue vari capace non è definito nel codice che si definisce in funzione.

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

Non hanno dichiarato un blocco set per Current proprietà, quindi è di sola lettura. È necessario per la sua attuazione, qualcosa come:

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

sì non ho dichiarato un blocco predefinito per Current perché quando si implementa l'interfaccia IEnumerator lo studio visivo genererà la proprietà corrente senza un set blocco oggetto pubblico Current { get {throw new NotImplementedException(); } } –

3

(Per all'indietro VB6 ragioni di compatibilità/Legacy) VB.NET consente di restituire i valori da una Property o Function impostando il nome Function/Property ad un valore.

Dai documenti: 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..."

esempio

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

Questo è più o meno equivalente a:

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

Quindi nel tuo codice VB, è l'impostazione del nome di proprietà, al fine di restituire un valore:

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

o nel tuo caso :

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

Ther e non è diretto equivalente a questo in C# come return restituirebbe immediatamente. Il miglior Così l'equivalente C# sarebbe quella di impostare una variabile temporanea e tornare che:

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

Ogni volta che accedere a una proprietà all'interno della classe in VB, ho sempre premettere mie proprietà con Me. che evita questo comportamento brutta