ReDim在C#中列出<t> </t>

时间:2012-08-09 19:56:13

标签: c# vb6

我理解的方式,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))
    {

    }
}

2 个答案:

答案 0 :(得分: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 ;)
}

答案 1 :(得分:0)

ReDimList不同。 ReDim允许您修改数组的大小。这允许您使用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;
}