Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

iLogic - How to return double zeros?

3 REPLIES 3
Reply
Message 1 of 4
Anonymous
424 Views, 3 Replies

iLogic - How to return double zeros?

Bear with me here, I'm not that proficient in ilogic or any sort of coding for that matter... hopefully I've got the terminology correct or at least correct enough to be understood.
Also, I do have a work-around for what I want to achieve but I'm curious to see if there is a better way of getting what I want.

 

My new work place has a semi-smart numbering system that looks like this:  xx-xxxxxx
The two digit prefix is for category/type of part and the six digit number after the hyphen is simply a dumb sequential part number.

 

But recently I've found a snag when trying to separate the two parts of the part number, so that the can be used in other ways that we don't need to cover here.
In the past when using ilogic I've used Val when needed to return the first numerical value from a string.

For example: 

 

Dim Category = Val(iProperties.Value("Project", "Part Number"))

This will return the numerical value to the left of the hyphen. No problem ... usually ....

 

.... I have now discovered that Val drops all proceeding zeros.

The prefixes of the part number starts at "01" and any numbers starting with the prefix "01" through "09" are returned as 1, 2, 3, 4 ... etc and not 01, 02, 03, 04 ....


I could of course use Left:

Dim Category = Left ((iProperties.Value("Project", "Part Number")),2)

But using Val provides a numerical value which in a way provides a kind of built in check that what ever has been entered as the part number is actually a number.
For example, if someone had mistakenly entered "p3" as the prefix, using Val will simply return 0.

 

Using Left will return "p3" but I'm not interested in using p3 as that is incorrect.
I can add a check by using IsNumeric but this adds an extra step and just doesn't seem as tidy as using just Val.


So, my question is, can Val be told to return proceeding zeros? Or is there some other way to return proceeding zeros as part of a numerical value instead of returning a string and then checking if it is numerical.

TIA, Ben

3 REPLIES 3
Message 2 of 4
JhoelForshav
in reply to: Anonymous

Hi @Anonymous 

You can use ToString to format the value 🙂

See example:

Dim pNum As String = "05-123456"
Dim Category As String = Val(pNum).ToString("00")
MsgBox(Category)

 

 

Message 3 of 4
Anonymous
in reply to: JhoelForshav

Hi @

 

 

 

Message 4 of 4
J-Camper
in reply to: Anonymous

If you Use CInt with some error handling, it will kick out invalid part numbers and can be converted into 2 character string:

Dim pNum As String = "05-123456"
Try
	Dim Category As String = CInt(Left(pNum, 2)).ToString("00")
	MsgBox(Category)
Catch
	MsgBox("Invlaid PartNumber")
End Try

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report