Best way of checking if input is a Positive Long?

To all



I am trying to set up a few checks on user supplied inputs. One of the inputs is a element ID (in a FemPart). I am wondering what is the "neatest" way of checking that the input is a Positive Long?



Rather that testing for "everything" I thought one could have




If ElementID Is Not a Positive Long Then
Exit sub
End if

It seems that CLng() is not recognized in a journal



The input can be anything (!). For example testing with a string




'------------------------
Imports NXOpen

Module test1
sub Main()

dim xlsinput as string = "1234.56"
Dim lgElemID as Long = CType(xlsinput, Long)

Dim theSession As Session = Session.GetSession()
Dim theLW As ListingWindow = theSession.ListingWindow

theLW.Open()
theLW.WriteLine ("--" & lgElemID ) ' returns 1235 !!!!!

end sub
end Module

Thanks



Regards



JXB

How are you gathering this info from the user?

If you are using an input box, you can't do much other than validate what the user has entered. An input box will return the input as a string; you can use the IsNumeric function to see that the input can be parsed into a number.
http://msdn.microsoft.com/en-us/library/6cd3f6w1%28v=vs.90%29.aspx
However, it won't guarantee that it is an integer (or long integer), simply that it represents a numeric value.
In the example given "1234.56" was rounded to the nearest integer. Perhaps it should have been truncated to "1234", or perhaps the decimal point was a mistake and it should be removed to give "123456". It isn't clear how that input should be interpreted. You can use various functions to manipulate the input and ensure it is an integer value, but we can never be sure that your interpretation matches the user's intent.

If you are using a custom input form, you can limit the user's input. For example, in a custom input form, you can ignore any keystrokes other than 0-9 and limit the number of keystrokes so as to not exceed the maximum integer (or long) value.

Perhaps better still would be to allow the user to select the nodes of interest from a list of eligible nodes, or select the nodes directly on the model.

Thanks NXJournaling for the reply.



I have just been thinking about your question: How are you gathering this info from the user?
when I realized that I knew the answer all along but was thinking too "hard". The input come from an Excel document ( a table of name(s) and numbers. One of the columns contains the element IDs to process. I can catch the potential "error" at the source by using the 'Data Validation' option in Excel ( 'Whole number' and 'greater than 1' ).



Obviously if the user really wants to "break" the macro by removing the 'Data Validation' in the xls input template and enter "silly" input then I am done!



Regards



JXB



So I think that for the moment the 'Data Validation' will do

Thanks
Regards