1

having a few issues trying to convert this code snippet to C#

   Private Shared Function GetOccInt(ByVal itm As String) As Integer
      Dim pos As Integer = InStr(itm, ">")
      Dim strTemp As String = Trim(Replace(Right(Trim(itm), itm.Length - pos), "</b>)</a>", String.Empty))
      pos = InStr(strTemp, ">")
      Return Convert.ToInt32(Trim(Right(strTemp, strTemp.Length - pos)))
   End Function

My final attempt which should be right is, but clearly isn't:

  private static int GetOccInt(string itm)
    {
        var pos = itm.IndexOf(">", StringComparison.Ordinal);
        var strTemp = itm.Trim().Substring(itm.Length - pos, itm.Length).Replace("</b>)</a>", "");
        pos = strTemp.IndexOf(">", StringComparison.Ordinal);
        return Convert.ToInt32(strTemp.Trim().Substring(strTemp.Length - pos, strTemp.Length));
    }

when debugging, I'm getting an argument out of range error for the substring function, message = Index and length must refer to a location within the string. Parameter name: length

The first time the breakpoint hit, the index was right (12) and the itm.Length said 31 and it appeared to have 31 charachters, but since itm.length is system code not sure how that could generate an error.

2
  • 1
    The original VB version is faulty (and might trigger errors). The Right method takes the trimmed version of itm (Trim(itm)) as argument but accounts for the un-trimmed length (itm.Length). It should also account for the trimmed length (Trim(itm).Length). Commented Oct 10, 2015 at 12:11
  • Did you forget to use Eval and Len in your vb ".net" code? ;) Commented Oct 10, 2015 at 12:16

2 Answers 2

3

VB

Right(Trim(itm), itm.Length - pos)

corresponds to C#

itm.Trim().Substring(pos)
Sign up to request clarification or add additional context in comments.

2 Comments

Note that VB Strings.Trim() only trims spaces, not other white space, so the .NET String equivalent is itm.Trim(' ').Substring(pos).
Also note that itm.Trim() can cause a null value exception.
3

Why reinvent the wheel? You can add a reference to the Microsoft.VisualBasic library and just use Strings.Right.

If you don't want to add that reference, the canonical replacement for Right(str, i) is str.Substring(str.Length - i) (only one argument, you use two in your example).

Note, though, that Right supports a target length exceeding the length of the string, whereas the replacement does not: Right("abc", 5) will yield abc in VB, but using "abc".Substring(3 - 5) will yield an exception.

6 Comments

I don't think that it would be reinventing the wheel, but moving to the new .NET format. The OP's code is a mixture of old VB and new VB.NET; why not fully relying on .NET and using Substring in VB.NET too?
Originally I was going to do that but I am trying to properly learn C# so I can stop using vb.
@dinotom: the Strings class is just a much a part of the framework as the String class. The name of the namespace does not actually make it vb only, or in any way improper to use it in C#. The Trim function safely deals with nulls, which can make it a better choice than the instance method.
@dinotom: Everything you said is absolutely correct, but it feels so wrong to use VB string functions in C#.
I guess that we have different approaches to this problem. I personally try to avoid all the methods in the Microsoft.VisualBasic namespace (and also the old VB ones which are still supported by NET, like Len) as much as possible. If you want to convert a VB6 code to .NET (VB.NET or C#, it doesn't matter), why not doing it properly, by completely relying on the new format (which is used across all the languages in the .NET Framework)? Additionally, most of the old VB string-manipulations methods are 1-based rather than 0-based what might provoke further confusions.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.