Developers Archive for the 'Visual Basic' Category

Number Functions - Conversion Functions

Number Functions - Conversion Functions Friday, March 9th, 2007

To retrieve any numbers present in a string the Val Function can be used. The Val function uses the following syntax:

Val(String)

The following code retrieves any numbers present in the given String, and adds it into a textbox

Dim Num As String
Num = “Year: 2007, date: 25″
Text1.Text = Val(Num)  ‘ Text1 will contain 200725

The Int function converts a number to integer value(whole number). The following code converts the decimal number to a whole number.

Dim Num As Single
Num = 123.45
Msgbox Int(Num)

o/p : 123

Other Functions:

Int - Converts a number to a Integer
CLng - Converts a number to a Long
Hex - Converts a number to Hex

Convert Binary to Decimal and vice versa

Convert Binary to Decimal and vice versa Wednesday, March 7th, 2007

//Function which converts decimal to binary
Private Function Dec2Bin(ByVal dec As Double) As String
   Dec2Bin = “”
   While dec >= 1
     Dec2Bin = IIf(dec Mod 2 = 0, “0″, “1″) & Dec2Bin
     dec = dec / 2
   Wend
End Function

//Function which converts binary to decimal
Private Function Bin2Dec(ByVal str As String) As Double
   Dim x As Integer
   Bin2Dec = 0
   For x = 1 To Len(str)
     Bin2Dec = Bin2Dec + (CInt(Mid(str, x, 1)) * (2 ^ (Len(str) - x)))
   Next x
End Function

Select Case statement

Select Case statement Tuesday, March 6th, 2007

The Select Case statement is an alternative to the If…Then…Else statement. It is especially used when testing lots of different conditions.
Syntax
The Select Case statement uses the following syntax:

Select Case expression
   Case expressionlist1
      statements
   Case expressionlist2
      statements
   Case Else
      statements
End Select

expression is a single expression, that is evaluated at the top of the list. The expressionlist then lists possible conditions and if the expression is equal to this case, the code in that section is run.

Description
Sub Command1_Click()

   Select Case Text1.Text
      Case “Hello”
         Msgbox “You entered Hello!”
      Case “Welcome”
         Msgbox “You entered welcome!”
      Case Else
         Msgbox “You entered something else”
   End Select
End Sub

This retrieves the value of Text1.Text and then tests it against each case. If Text1.Text is equal to “Hello” then a message box is displayed saying “You entered Hello”.  If Text1.Text is equal to “Welcome” then a message box is displayed saying “You entered welcome!”. If Text1.Text is equal to something else then a message box is displayed saying “You entered something else”.

‘ Initialise variable
Dim Number As Integer
Number = 8
Select Case Number    ‘ Evaluate Number.
Case 1 To 5    ‘ Number between 1 and 5.
    Msgbox “Between 1 and 5″
Case 6, 7, 8    ‘ Number between 6 and 8.
    Msgbox “Between 6 and 8″
Case Is > 8 And Number < 11    ‘ Number is 9 or 10.
    Msgbox “Greater than 8″
Case Else    ‘ Other values.
    Msgbox “Not between 1 and 10″
End Select

This code evaluates the variable Number, and then runs the appropriate code. Note you need to use the statement Is if you are using comparison operators (i.e. = ,<=, > )


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.