none
Correct method for greater than 1 RRS feed

  • Question

  • Hi

    I'm working on some Autodesk Inventor coding. I'm currently trying to prevent duplication of part names being entered into a table. Each part name is made up like this:

    MyPart:1

    Inventor itself will add the ':1' to it so that multiple numbers can be identified for the same part

    • MyPart:1
    • MyPart:2
    • MyPart:3

    the method I am using will take the name and attempt to get the number from the string however, I am coming up against a few issues which are probably simple to fix if I had the right method......

    If I use the CDbl(Microsoft.VisualBasic.Right(String,1)) method, it only works from 1 to 9. This is because 11,21,31,41 etc etc will still have 1 as the last character. So, I tried CDbl(Microsoft.VisualBasic.Right(String, 2)) but then on numbers 1 to 9, I also capture the : which isn't a double so it obviously throws an error.

    I tried

    Dim pName As String = Part.Name ' MyPart:1
    Dim pCnt As Double = CDbl(InStrRev(pName, pName, ":")
    but that still gets everything to the left of : when I need everything to the right.

    Is there an easy way of getting the number after the : irrespective of character count? The number can be anything between 1 and 100,000. All I'm going to do is ignore anything over the value of 1 which will give me the first instance of the part MyPart:1 and nothing else

    Cheers

    Nacho


    Nacho is the derivative of Nigel - True fact! I am self taught in VB.Net. 50% of the time, I am right 100% of the time!

    Sunday, March 5, 2017 3:29 AM

Answers

  • Example:

    Dim pName As String = "MyPart:123"
    Dim num As String = pName.Substring(pName.LastIndexOf(":") + 1)

    I leave it to you to make sure there is a colon present.

    - Wayne



    • Marked as answer by NachoShaw Sunday, March 5, 2017 11:13 PM
    • Edited by WayneAKing Monday, March 6, 2017 10:31 AM
    Sunday, March 5, 2017 3:43 AM

All replies

  • Example:

    Dim pName As String = "MyPart:123"
    Dim num As String = pName.Substring(pName.LastIndexOf(":") + 1)

    I leave it to you to make sure there is a colon present.

    - Wayne



    • Marked as answer by NachoShaw Sunday, March 5, 2017 11:13 PM
    • Edited by WayneAKing Monday, March 6, 2017 10:31 AM
    Sunday, March 5, 2017 3:43 AM
  • You could try using the Split function.

    Yep. That's a good choice:

    Dim sa As String() = pName.Split(":")

    - Wayne

    Sunday, March 5, 2017 4:22 AM
  • Hi

    thanks for the reply. I had actually tried this method but hadn't added the +1 at the end

    the colon is always present

    cheers

    nacho


    Nacho is the derivative of Nigel - True fact! I am self taught in VB.Net. 50% of the time, I am right 100% of the time!

    Sunday, March 5, 2017 11:15 PM