How To

How to Write a Subroutine in VB.net

VB.net
VB.net
Member
By Mike
eHow Community Member
(0 Ratings)

Subroutines give you the ability to write code that can be used over and over again in a program. This saves you time from writing the same code more than once. In this example I am writing a subroutine that will sort some numbers.

Difficulty: Moderately Easy
Instructions

Things You'll Need:

  • VB.net
  • basic programming skills
  1. Step 1

    Open VB.net

  2. Step 2

    Create a new project

  3. Step 3
    Sort Button
    Sort Button

    Place a button on the form and change its text property to "Sort".

  4. Step 4
    Listbox
    Listbox

    Place a listbox on the form. This is where we will display the sorted list.

  5. Step 5
    Click Event
    Click Event

    Double click on the button control to bring up the code window and the button's click event.

  6. Step 6
    Global Variable
    Global Variable

    Directly above the click event subroutine type in:
    Public aryNumberList(5) As Integer

    This creates our public global variable array that we will use in our example to hold our numbers.

  7. Step 7
    Loop variable
    Loop variable

    In the button's click event delcare a loop variable. This will be used to add our array items after they have been sorted the our listbox.

  8. Step 8
    Assign numbers
    Assign numbers

    Add numbers in a random order to our array variable that we declared in step 6.

  9. Step 9
    Subroutine
    Subroutine

    Before we do anything else we must now create our sorting subroutine. Remember this is just an example so to keep it simple I am using a basic swapping sort. You can see the code in the picture.

  10. Step 10
    Call Subroutine
    Call Subroutine

    Go back into the click event and call the subroutine passing the global array variable as a parameter.

  11. Step 11
    Display the sorted list
    Display the sorted list

    Display the sorted list in the listbox.

  12. Step 12

    Press the "F5" key on your keyboard to run the application.

  13. Step 13

    Click the sort button.

  14. Step 14
    Final Results
    Final Results

    Your listbox should now display the numbers in their proper order.

Resources

Post a Comment

Post a Comment

Have you done this? Click here to let us know.

I Did This

Related Ads

Computers
Alexia Petrakos,

Meet Alexia Petrakos eHow’s Computers Expert.

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy.   en-US

eHow Computers
eHow_eHow Technology and Electronics