My Favourite Functions - Part 3
Check for Whole Numbers
Here's a groovy little one liner I wrote which checks whether a number is an integer - it returns True for a number such as 5, yet False for 2.5
Assuming the text you're wanting to analyse is residing in a text box called 'txtBox1', here's the code:
binteger = (txtBox1.Text - CInt(txtBox1.Text)) = 0
-- The Mysterious Martinus Pardede
There's a nice little-known function in Visual Basic called Choose(). It's great when you need to do comparisons but can't be bothered writing an entire Select Case statement.
Here's an example:
MsgBox Choose(2, "One", "Two", "Three")
Here, I'm passing Choose() the number 2, followed by three 'choices'. In this instance, Choose() returns the second option - 'Two'. You can have as many options as you wish.
Here's the official Choose() function format:
Destination = Choose(index, choice-1[, choice-2, ...[, choice-n]])
-- Janina Barrister, Mexico
Pad Out a String
If you've ever needed to pad out a filename (or other string) to a set number of characters, you might be interested in this Fill function I wrote. It works with virtually all versions of Visual Basic, and is particularly useful for those without the luxurious frills of version 6. Here's how it works:
x = Fill("world.txt", 8, "0") '----returns------> 000world.txtx = Fill("world.txt", 8) '----returns------> 752world.txt
In the first example, we pass it a fill character of 0. In the second, we don't specify anything, so it fills with random numbers. Have a go with it yourself to test.
Here's the code you'll need to add to your form or module for it to work:
Public Function Fill(ByVal sFln As _String, iQty As Integer, _Optional sChar As String) As StringDim SF As StringDim srnd As StringSF = sFlnsrnd = sCharDim iSize As IntegeriSize = Len(SF)If iSize > iQty ThenFill = sFlnElseIf iSize = iQty ThenFill = SFElseFor i = 1 To iQty - iSize If sChar = "" Then srnd = Fix(Rnd() * 10)End IfPrefix = Prefix & srndNext iFill = Prefix & SFEnd IfEnd Function
You might also be interested in the Space() function that bundles with Visual Basic it returns a certain number of spaces, also useful for padding out a string, or maybe displaying information that requires a certain number of spaces between words. It also useful when you need to 'make room' for variables you're passing to the API. Check it out in the help if you're interested.
-- Jose Alonso Yocupicio Zazueta<Ed pauses for breath, then continues to page three...>
Page 3 of 5