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.

dinotom
  • 4,990
  • 16
  • 71
  • 139
  • 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`). – varocarbas Oct 10 '15 at 12:11
  • Did you forget to use `Eval` and `Len` in your vb ".net" code? ;) – Bjørn-Roger Kringsjå Oct 10 '15 at 12:16

2 Answers2

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.

Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • 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? – varocarbas Oct 10 '15 at 12:13
  • Originally I was going to do that but I am trying to properly learn C# so I can stop using vb. – dinotom Oct 10 '15 at 12:48
  • 2
    @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. – jmoreno Oct 10 '15 at 22:19
  • @dinotom: Everything you said is absolutely correct, but it feels so wrong to use VB string functions in C#. – Dave Doknjas Oct 11 '15 at 02:39
  • @varocarbas: That's usually what I try to do when developing in VB.NET. However, sometimes there is no equivalent method in the .NET framework, and `Left` and `Right` (= take *at most* x characters from the left or right) are prime examples for that. Yes, you can recreate them with `String.Substring`, `Math.Min` and `String.Length`, but that would be redeveloping a well-tested and supported BCL method. Of course, as soon as the framework gets a [`String.Truncate` method](http://stackoverflow.com/q/2776673/87698), I'll stop using `Left`. – Heinzi Oct 11 '15 at 08:07
  • 1
    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. – varocarbas Oct 11 '15 at 08:17
3

VB

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

corresponds to C#

itm.Trim().Substring(pos)
Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343