views:

63

answers:

3

I have converted this existing C# .NET 2.0 code (which appears to take in a string with any characters and return only the numbers in that string):

    private static string StripMIN(string min)
    {
        string result = string.Empty;
        int digit = 0;
        for (int i = 0; i < min.Length; i++)
        {
            if (int.TryParse(min[i].ToString(), out digit) == true)
            {
                result += min[i];
            }
        }
        return result;
    }

to this VB.Net .Net 1.1 code:

    Private Function StripMIN(ByVal min As String) As String
        Dim result As String = String.Empty
        Dim digit As Integer = 0
        Dim i As Integer = 0
        While i < min.Length
            Me.SystemMessages.Text = "Char at " + i + " : " + min(i)
            If TryParseInteger(min(i).ToString(), digit) = True Then
                result += min(i)
            End If
            System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
        End While
        Return result
    End Function

I am getting an error message on line 6 of the VB.Net code at min(i). The error message reads:

"Expression is not an array or a method, and cannot have an argument list"

I am not very well versed in .Net 1.1 and cannot find an alternative solution to solving this error. Does anybody have any suggestions on how I can access each character of this string and verify that its a number and return only the numbers of the string in .Net 1.1?

+6  A: 

Your issue is converting to VB, not a framework change. Try using min.Chars(i). See this MSDN article, which shows that the indexer property (where you see min[i] in C#) is named Chars in VB)

However, I would use something like this instead:

Private Function StripMIN(ByVal min As String) As String
    Dim result as New StringBuilder()

    ForEach c In min
        If Char.IsDigit(c) Then
            result.Append(c)
        End If
    End ForEach

    Return result.ToString()
End Function
Adam Robinson
A: 

You should try to use the Substring method on the variable of min. Something like this:

Me.SystemMessages.Text = "Char at " + i + " : " + min.Substring(i, 1)

I think that is correct. Give it a try and let me know what you find. I'm used to C#

Jeff
Using `Substring` here would be inefficient and detracts from readability. `Substring` creates a new string rather than simply returning a single `Char`.
Adam Robinson
+1  A: 

The default indexer min[x] in C# corresponds to the named indexer min.Chars(x) in VB.

However, you don't need to loop the characters in the string. You can use a regular expression that matches all non-digit characters to remove them:

Private Function StripMIN(ByVal min As String) As String
   Return Regex.Replace(min, "[^\d]+", String.Empty)
End Function
Guffa