I am writing a library in VB.NET in which I have added, among others, a class originally written in C# but converted into VB.NET. I don't know much about C# so therefore I have used online C# to VB.NET-converters. Now I am stuck with some code which I believe the online-converter was not able to "translate" properly.
When running the code I get the following error:
System.IndexOutOfRangeException was unhandled
Message="IndexOutOfRangeException"
StackTrace:
at System.String.get_Chars()
.........
I really don't understand the reason for this error. I believe this error might be due either: - to the fact that C# is able to automatically convert an integer variable into a string (while VB.NET needs the "toString-method") - or due to the fact C# is using an incremental operator which is not supported by VB.NET.
Here is the original code-snippet in C# where "m_primaryKey" is a StringBuilder-object:
private void addMetaphoneCharacter(String primaryCharacter, String alternateCharacter)
{
//Is the primary character valid?
if (primaryCharacter.Length > 0)
{
int idx = 0;
while (idx < primaryCharacter.Length)
{
m_primaryKey.Length++;
m_primaryKey[m_primaryKeyLength++] = primaryCharacter[idx++];
}
}
//other code deleted
This original code works when using a class-library created in C#.
Here is the converted code in VB.NET which gives me the error mentioned earlier:
Private Sub addMetaphoneCharacter(ByVal primaryCharacter As String, ByVal alternateCharacter As String)
'Is the primary character valid?
If primaryCharacter.Length > 0 Then
Dim idx As Integer = 0
While idx < primaryCharacter.Length
m_primaryKey.Length += 1
m_primaryKey(System.Math.Max(System.Threading.Interlocked.Increment(m_primaryKeyLength), _
m_primaryKeyLength - 1)) = primaryCharacter _
(System.Math.Max(System.Threading.Interlocked.Increment(idx), idx - 1))
End While
End If
'other code deleted
The original code may be found here.
I should say that the code in the class is quite advanced for me (I'm a hobby programmer but learning every day) so maybe I don't see obvious things but this is the reason why I am asking you.
Can you please give me any hints to sort out this problem?
Thank you.
EDIT: Here is the complete sub in C#:
/**
* Appends a metaphone character to the primary, and a possibly different alternate,
* metaphone keys for the word.
*
* @param primaryCharacter
* Primary character to append to primary key, and, if no alternate char is present,
* the alternate key as well
* @param alternateCharacter
* Alternate character to append to alternate key. May be null or a zero-length string,
* in which case the primary character will be appended to the alternate key instead
*/
private void addMetaphoneCharacter(String primaryCharacter, String alternateCharacter)
{
//Is the primary character valid?
if (primaryCharacter.Length > 0)
{
int idx = 0;
while (idx < primaryCharacter.Length)
{
m_primaryKey.Length++;
m_primaryKey[m_primaryKeyLength++] = primaryCharacter[idx++];
}
}
//Is the alternate character valid?
if (alternateCharacter != null)
{
//Alternate character was provided. If it is not zero-length, append it, else
//append the primary string as long as it wasn't zero length and isn't a space character
if (alternateCharacter.Length > 0)
{
m_hasAlternate = true;
if (alternateCharacter[0] != ' ')
{
int idx = 0;
while (idx < alternateCharacter.Length)
{
m_alternateKey.Length++;
m_alternateKey[m_alternateKeyLength++] = alternateCharacter[idx++];
}
}
}
else
{
//No, but if the primary character is valid, add that instead
if (primaryCharacter.Length > 0 && (primaryCharacter[0] != ' '))
{
int idx = 0;
while (idx < primaryCharacter.Length)
{
m_alternateKey.Length++;
m_alternateKey[m_alternateKeyLength++] = primaryCharacter[idx++];
}
}
}
}
else if (primaryCharacter.Length > 0)
{
//Else, no alternate character was passed, but a primary was, so append the primary character to the alternate key
int idx = 0;
while (idx < primaryCharacter.Length)
{
m_alternateKey.Length++;
m_alternateKey[m_alternateKeyLength++] = primaryCharacter[idx++];
}
}
}