Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is it Numeric? 3

Status
Not open for further replies.

sanders720

Programmer
Aug 2, 2001
421
0
0
US
Can someone please tell me what I am doing wrong here? I simply want to get the first characters of a string until they become non-numeric and apply the quantity variable Q.

Thanks in advance for the help!


Public Sub IsItNumeric(ByVal ConStr As String)
' Example Data is 12XXX or 12
Dim x As Int16
For x = 1 To ConStr.Length
MsgBox(x)
Try
' IsNumeric(ConStr.Substring(1, x))
Q = Convert.ToInt16(ConStr.Substring(1, x))
MsgBox(Q)

Catch ex As Exception
Q = ConStr.Substring(1, x)
MsgBox(Q)

End Try

Next

End Sub
 
Exactly, what if you wanted 120000 as a valid number?

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
I'm sorry, but I don't follow much of this dialogue. The quantity I am using will either have a number, or a number with XXX after it. XXX means exclude. This numeric value is used in Autodesk Inventor, in an assembly drawing. The name of the waaembly is followed by :12XXX. Ste string I am passing is 12XXX, and I qould like to assign global variable Q to 12 in this case.

My question is, does the IsNumeric command evaluate strings, or is it looking for the number? Is the case below true?

IsNumeric "12" = True
IsNumeric "12X" = False?

Thanks for your reply
 
I assumed you included the 'XXX' part to indicate that any sort of string could follow the number. If they will ALWAYS be X's, then you should be able to do the following:

Dim S as string = "12XXX"
Dim Q as Integer

Q = CInt(S.Replace("X", ""))

Basically, you replace the "X" in the string with an empty string, which effectively removes the X's. You are left with a string that can be converted to an integer.

Hope this helps.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
I know nothing about 'Autodesk Inventor'. After 5 minutes of looking at their website, I'm going to make a wild guess that you are actually using VBA within AutoDesk.

If this is true, then the code should read:

Q = CInt(Replace(S, "X", ""))



-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
So long as these are whole, positive numbers below the int16 max (32767) then this works.

Code:
    Private Function GetTextUpToNum(ByVal InputString As String) As Int16

        Dim OutputString As String = ""
        For Each c As Char In InputString

            If Asc(c) < 58 And Asc(c) > 47 Then
                [COLOR=green]'char is in range "0" to "9"[/color]
                OutputString = OutputString & c
            Else
                Exit For
            End If
        Next
        Try
            If OutputString <> "" Then Return Convert.ToInt16(OutputString)
        Catch ex As Exception
            MessageBox.Show(ex.Message) [COLOR=green]' eg if number > 32767[/color]
        End Try
        Return -1 [COLOR=green]'started with text.[/color]

    End Function
 
No, VB.NET externally using what they call Apprentice. I will try the replace option tomorrow.

Thanks for all your help.
 

I would still like to ask - for future reference, does the IsNumeric command evaluate strings, or is it looking for the number? Is the case below true?

IsNumeric "12" = True
IsNumeric "12X" = False?

Thanks for your reply.
 
Is numeric looks for any non-numeric character in a string. so your statement is true, and very easily testable.

Code:
messagebox.show("'12':" & IsNumeric("12"))
messagebox.show("'12X':" & IsNumeric("12X"))

-Rick


VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
That explains why If Not IsNumeric was used to get numeric numbers. This seems opposite of what it should be.

Thanks
 
I was using the not to determine when the index was pointing at a non-numeric character. At that point it should collect all characters up to that point and return them. If it never runs into that point, it should return all records. But if the non-numeric characters will always be "x" then using the string.replace method is going to be the best route.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top