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
Provare a sostituire 'Current = this.Sentence.Substring ...' con 'var functionReturnValue = this.Sentence.Substring ...' –
@YacoubMassad grazie a mr yacoub ma Perché sono costretto a scrivere "function Return Value" Voglio per capire esattamente perché si è verificato questo errore –
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. –