Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 7 - Learning the Visual Basic .NET Language
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC.NET

Learning the Visual Basic .NET Language
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 25
    2005-11-17

    Table of Contents:
  • Learning the Visual Basic .NET Language
  • The Evolution of VB .NET
  • Variables and Data Types
  • Arrays
  • Variable Operations
  • The String Class
  • Conditional Structures
  • Loop Structures
  • Functions and Subroutines

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Learning the Visual Basic .NET Language - Conditional Structures


    (Page 7 of 9 )

    In many ways, conditional logic—deciding which action to take based on user input, external conditions, or other information—is the heart of programming.

    All conditional logic starts with a condition: a simple expression that can be evaluated to True or False. Your code can then make a decision to execute different logic depending on the outcome of the condition. To build a condition, you can use any combination of literal values or variables, along with logical operators. Table 3-7 lists the basic logical operators.

    Table 3-7. Logical Operators

    Operator

    Description

    =

    Equal to

    <>

    Not equal to

    <

    Less than

    >

    Greater than

    <=

    Less than or equal to

    >=

    Greater than or equal to

    And

    And (evaluates to True only if both expressions are True)

    Or

    Or (evaluates to True if either expression is True)

    You can use the comparison operators (<, >, <=, >=) with numeric types and strings. A string is deemed to be “less than” another string if it occurs earlier in an alphabetic sort. Thus “apple” is less than “attach.”

    The If End If Block

    The If … End If block is the powerhouse of conditional logic, able to evaluate any combination of conditions and deal with multiple and different pieces of data. Here’s an example with an If … End If block that features two conditions:

    If MyNumber > 10 Then
        ' Do something.
    ElseIf MyString = "hello" Then
        ' Do something.
    Else
       
    ' Do something.
    End If

    Keep in mind that the If … End If block matches one condition at most. For example, if MyNumber is greater than 10, the first condition will be met. That means the code in the first conditional block will run, and no other conditions will be evaluated. Whether or not MyString contains the text “hello” becomes irrelevant, because that condition will not be evaluated.

    The Select Case Block

    VB .NET also provides a Select Case structure that you can use to evaluate a single variable or expression for multiple possible values. In the following code, each case examines the MyNumber variable, and tests if it’s equal to a specific integer.

    Select Case MyNumber
        Case 1
            ' Do something if MyNumber = 1.
        Case 2
            ' Do something if MyNumber = 2.
        Case Else
            ' Do something if MyNumber is anything else.
    End Select

    If desired, you can handle multiple cases with one segment of code by including a list of comma-separated values in the Case statement.

    Select Case MyNumber
        Case 1, 2
            ' Do something if MyNumber = 1 Or MyNumber = 2.
        Case Else
            ' Do something if MyNumber is anything else.
    End Select

    Unlike the If … End If block, Select Case is limited to evaluating a single piece of information at a time. However, it provides a leaner, clearer syntax than the If … End If block for situations in which you need to test a single variable.

    More Visual Basic.NET Articles
    More By Apress Publishing


       · This article is an excerpt from the book "Beginning ASP.NET in VB.NET: From Novice...
     

    Buy this book now. This article is excerpted from chapter three of the book Beginning ASP.NET in VB.NET: From Novice to Professional, written by Matthew MacDonald (Apress, 2004; ISBN: 1590592786). Check it out at your favorite bookstore today. Buy this book now.

    VISUAL BASIC.NET ARTICLES

    - User-defined Functions using Visual Basic Ap...
    - Understanding Object Binding in VBA
    - Mastering the Message Box
    - Testing a Windows Forms Application
    - Using Visual Basic.NET Features to Code a Wi...
    - Correcting Code in a Windows Forms Applicati...
    - Write Readable Code and Comments for Windows...
    - How to Code and Test a Windows Forms Applica...
    - Adding Features to a Windows Forms Applicati...
    - How to Design a Windows Forms Application
    - LINQ to XML Programming Using Visual Basic.N...
    - Understanding Delegates using Visual Basic.N...
    - Create a Sudoku Puzzle Generator using VB.NET
    - Entity Creation and Messaging in a VB.NET Te...
    - Movement and Player Statistics in a VB.NET T...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek