Monday 17 March 2014

What Is If TypeOf Statement and IIf Function in Visual Basic?

If TypeOf Statement and IIf Function in Visual Basic


If TypeOf Statement:


If TypeOf Statement allows conditional execution based on processing to a control Type. Syntax of If TypeOf Statement is

If TypeOf ControlName is ControlType Then
          [StatementBlock 1]
[ElseIf TypeOf ControlName is ControlType Then
          [StatementBlock 2]
[Else
          [Statement Block n]]
End If

Where ControlName is the valid name of control and ControlType must also be valid one.



Exapmle:



Start Visual Basic with new Standard EXE project, place a CommandButton and a TextBox control on a Form and write following code in the Click event of the CommandButton.


Private Sub Command1_Click ()
      If TypeOf Text1 is Textbox Then
            Msgbox (“Textbox”)
      End If
End Sub


Press F5 key to run this project and Click the CommandButton. The result of above code will be in the Form of MsgBox function.


IIf Function:


IIf Function is closely related with If – Then – Else – structure. The syntax of IIf function is

         IIf ( Condition, Value1, Vlaue2 )

Where value1 will be returned if condition is true otherwise value2 will be returned.

Example:

Start Visual Basic with new Standard EXE project, place a CommandButton on a Form and write following code in the Click event of the CommandButton control.

Private Sub Command1_Click ()
  Dim x As Integer
  X = 6
  Print IIf ( x > 5, “ Greater then 5 “ , “Less then 5 “ )


Press F5 key to run this project and Click the CommandButton to check its output.


Me : If you need more help, want to ask feel free to use below comment box. It will be my pleasure to help you. THANKS for reading.

0 comments:

 
Search Engine Submission - AddMe url submit Casino us org Seo Packages http://blogsiteslist.com