1

据我了解,Redim 类似于List<T>. 我可以使用帮助确保从这个 VB6 到 C# 的正确转换:

Private Sub ParseString(sInput As String, sWords() As String, lCount As Long, sDel As String)      
    '   Parses a delimited input string (sInput) on a single
    '   delimiter and returns the parsed words back in a
    '   string array sWords().
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '
    ' INPUTS:
    '   sInput - string to be parsed.
    '   sDel   - Delimiter character.
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' OUTPUTS:
    '   sWords() - dynamic string array containing the parsed words.
    '
    '   lCount   - long, returning the number of words parsed
    '
    ' NOTES:
    '   If this subroutine is passed an empty string, it will
    '   return a lCount of 0 with one element in sWords().
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Dim lWordStart As Long
    Dim lWordEnd As Long
    Dim sTemp As String
    Dim lParsedArraySize As Long
    Dim lDelLen As Long
    '    Dim lStartM As Long
    '    Dim lEndM As Long
    Dim lLength As Long

    lDelLen = Len(sDel)
    lLength = Len(sInput)

    If sInput = "" Then
       ReDim sWords(1 To 1) As String
       lCount = 0
       sWords(1) = ""
       Exit Sub
    End If
    lParsedArraySize = 50
ReDim sWords(1 To lParsedArraySize) As String

    lWordStart = 1
    lCount = 1

    Do
        lWordEnd = InStr(lWordStart, sInput, sDel)
        If lWordEnd = 0 Then
            sTemp = Mid$(sInput, lWordStart)

            If lCount > lParsedArraySize Then
            ReDim Preserve sWords(1 To lCount) As String
            End If
            sWords(lCount) = sTemp
            Exit Do
        Else
            sTemp = Mid$(sInput, lWordStart, lWordEnd - lWordStart)
            'If sTemp <> "" Then
            If lCount > lParsedArraySize Then
                lParsedArraySize = lParsedArraySize + 50
                ReDim Preserve sWords(1 To lParsedArraySize) As String
            End If
            sWords(lCount) = sTemp
            lCount = lCount + 1
            'End If
            lWordStart = lWordEnd + lDelLen
        End If
    Loop

    If lCount < lParsedArraySize Then
    ReDim Preserve sWords(1 To lCount) As String
    End If

我应该如何将此 If 语句转换为 C#?到目前为止,我已经...

private void ParseString(string sInput, List<string> sWords, int lCount, string sDel)
{
    int lWordStart;
    int lWordEnd;
    string sTemp;
    int lParsedArraySize;
    int lDelLen;
    //int lStartM;
    //int lEndM;
    int lLength;

    lDelLen = sDel.Length;

     lLength = sInput.Length;

    if(String.IsNullOrEmpty(sInput))
    {

    }
}
4

2 回答 2

2

您的函数调用似乎正在修改数组。如果是这样,这是您需要做的:

编辑:既然您提供了一些额外的细节,我认为您需要两个输出。

private void ParseString(string sInput, out List<string> sWords, out int lCount, string sDel)
{
    int lWordStart;
    int lWordEnd;
    string sTemp;
    int lParsedArraySize;
    int lDelLen;
    int lLength;

    lDelLen = sDel.Length;

    lLength = sInput.Length;

    // We are required to set all the output values in this function call    
    sWords = new List<string>();
    lCount = 0;

    if (String.IsNullOrEmpty(sInput)) {
        sWords.Add(""); // Now, sWords[0] will equal "" - this may not be exactly what
        // your VB code expects, but since all C# arrays begin with zero it's the
        // closest approximation.  Alternatively, you could add two items so that
        // sWords[1] would still return the correct value.
        return;
    }
    lParsedArraySize = 50;

    // I'm assuming there's more code down here that does more work ;)
}
于 2012-08-09T20:01:41.353 回答
0

ReDim不一样ListReDim允许您修改数组的大小。这允许您使用数组来模拟列表的可变大小行为ReDim

看起来您的逻辑正在测试输入是否为空并将该值分配给第一个输出元素。它似乎也首先清除列表(数组)。一种方法是:

if (sWords == null) {
  sWords = new List<string>(); // Ensure the list is not null
}
else {
  sWords.Clear()      // Clear the list if it was provided
} 

// Add an empty string to the list if the input was empty.
if (String.IsNullOrEmpty(sInput)) {
  sWords.Add(String.Empty);
  return;
}
于 2012-08-09T20:11:02.780 回答