.net-由于增量运算符或自动字符串转换,无法将代码从c#转换为vb.net(我认为)
我正在用VB.NET编写一个库,其中添加了一个最初用C#编写但转换为VB.NET的类。我对C不太了解,因此我使用了在线C到VB.NET转换器。现在我被一些代码卡住了,我相信在线转换器不能正确地“翻译” 在运行代码时,我遇到以下错误:.net-由于增量运算符或自动字符串转换,无法将代码从c#转换为vb.net(我认为),c#,.net,vb.net,C#,.net,Vb.net,我正在用VB.NET编写一个库,其中添加了一个最初用C#编写但转换为VB.NET的类。我对C不太了解,因此我使用了在线C到VB.NET转换器。现在我被一些代码卡住了,我相信在线转换器不能正确地“翻译” 在运行代码时,我遇到以下错误: System.IndexOutOfRangeException was unhandled Message="IndexOutOfRangeException" StackTrace: at System.String.get_Chars() ...
System.IndexOutOfRangeException was unhandled
Message="IndexOutOfRangeException"
StackTrace:
at System.String.get_Chars()
.........
我真的不明白这个错误的原因。我认为这一错误可能是由于:
-C#能够自动将整数变量转换为字符串(而VB.NET需要“toString方法”)
-或者由于C#使用的是VB.NET不支持的增量运算符
以下是C#中的原始代码段,“m#u primaryKey”是StringBuilder对象:
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
private void addMetaphoneCharacter(字符串primaryCharacter、字符串alternateCharacter)
{
//主字符有效吗?
如果(primaryCharacter.Length>0)
{
int-idx=0;
while(idx
这段原始代码在使用C#中创建的类库时有效
下面是VB.NET中转换的代码,它给出了前面提到的错误:
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
Private Sub addMetaphoneCharacter(ByVal primaryCharacter作为字符串,ByVal alternateCharacter作为字符串)
'主字符有效吗?
如果primaryCharacter.Length>0,则
Dim idx作为整数=0
而idx
可以找到原始代码
我应该说,课堂上的代码对我来说是相当先进的(我是一个爱好程序员,但每天都在学习),所以我可能看不到明显的东西,但这就是我问你的原因
你能给我一些提示来解决这个问题吗
多谢各位
编辑:
下面是C#中的完整子项:
/**
*将变音字符附加到主字符,以及可能不同的备用字符,
*单词的变音键。
*
*@param primaryCharacter
*要附加到主键的主字符,如果不存在备用字符,
*还有备用键
*@param alternateCharacter
*要附加到备用键的备用字符。可能为空或长度为零的字符串,
*在这种情况下,主字符将附加到备用键
*/
私有void addMetaphoneCharacter(字符串primaryCharacter、字符串alternateCharacter)
{
//主字符有效吗?
如果(primaryCharacter.Length>0)
{
int-idx=0;
while(idx0)
{
m_hasaAlternate=真;
if(可选字符[0]!='')
{
int-idx=0;
while(idx0&&(primaryCharacter[0]!='')
{
int-idx=0;
while(idx0)
{
//否则,未传递备用字符,但传递了主字符,因此将主字符附加到备用键
int-idx=0;
while(idx
索引越界意味着您试图访问数组大小以外的内容。这通常是由“超过一个”问题引起的
从代码上看,VB转换相当奇怪,并且正在做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++];
}
}
}
m_primaryKey(m_primaryKeyLength) = primaryCharacter(idx)
m_primaryKeyLength = m_primaryKeyLength + 1
idx = idx + 1
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(m_primaryKeyLength) = primaryCharacter(idx)
m_primaryKeyLength += 1
idx += 1
End While
End If
'other code deleted
m_primaryKey.Length += 1
m_primaryKeyLength += 1
idx += 1
m_primaryKey(m_primaryKeyLength) = primaryCharacter (idx)