2012-08-09 29 views
1

我的理解是,Redim类似于List<T>。我可以帮助确保从VB6到C#的正确转换:ReDim到列表<T>在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)) 
    { 

    } 
} 
+0

'的foreach(字符串在剑电流){...}' – 2012-08-09 19:57:52

+0

@AndreCalil'sWords'应该被'ParseString'填补。在空单上的“foreach”不会有太大的作用。 – hvd 2012-08-09 19:58:44

+0

@AndreCalil - 你在哪里拿出'foreach'?我不明白为什么我们在这个if语句中有'Exit Sub' ...这里有一个循环吗......? – 2012-08-09 19:59:56

回答

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 ;) 
} 
+0

我认为这可能是接近的,但是'Exit sub'没有被考虑。也许if语句只需要'return sWords';'VB中的 – 2012-08-09 20:06:26

+0

@ P.Brian.Mackey'Sub'就像C-Sharp中的'void'返回方法,所以它只是'return;'。 – Matthew 2012-08-09 20:07:32

+0

谢谢,补充说,改变。 – 2012-08-09 20:08:42

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; 
}