Creating Sub Procedures in VB.net

By: Steven Holzner Emailed: 1641 times Printed: 2109 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

We know all about Sub procedures: They're the handy blocks of code that can organize your code into single-purpose sections to make programming easier. Unlike functions, Sub procedures do not return values, but like functions, you can pass values to Sub procedures in an argument list.

You declare Sub procedures with the Sub statement:

[ <attrlist> ] [{ Overloads | Overrides | Overridable | NotOverridable |
MustOverride | Shadows | Shared }]
[{ Public | Protected | Friend | Protected Friend | Private }]
Sub name [(arglist)]
    [ statements ]
    [ Exit Sub ]
     [ statements ]
End Sub

Here are the parts of this statement:

  • attrlist-List of attributes for this procedure. You separate multiple attributes with commas.

  • Overloads-Specifies that this Sub procedure overloads one (or more) procedures defined with the same name in a base class. In this case, the argument list must be different from the argument list of every procedure that is to be overloaded (that is, the lists must differ in the number of arguments, their data types, or both). You cannot specify both Overloads and Shadows in the same procedure declaration.

  • Overrides-Specifies that this Sub procedure overrides a procedure with the same name in a base class. Note that the number and data types of the arguments must match those of the procedure in the base class.

  • Overridable-Specifies that this Sub procedure can be overridden by a procedure with the same name in a derived class.

  • NotOverridable-Specifies that this Sub procedure may not be overridden in a derived class.

  • MustOverride-Specifies that this Sub procedure is not implemented. Instead, this procedure must be implemented in a derived class. If it is not, that class will not be creatable.

  • Shadows-Makes this Sub procedure a shadow of an identically named programming element in a base class. A shadowed element is unavailable in the derived class that shadows it. You can use Shadows only at module, namespace, or file level (but not inside a procedure). This means you can declare shadowing variables in a source file or inside a module, class, or structure, but not inside a procedure. Note that you cannot specify both Overloads and Shadows in the same procedure declaration.

  • Shared-Specifies that this Sub procedure is a shared procedure. As a shared procedure, it is not associated with a specific instance of a class or structure, and you can call it by qualifying it either with the class or structure name, or with the variable name of a specific instance of the class or structure.

  • Public-Procedures declared Public have public access. There are no restrictions on the accessibility of public procedures.

  • Protected-Procedures declared Protected have protected access. They are accessible only from within their own class or from a derived class. Protected access can be specified only on members of classes.

  • Friend-Procedures declared Friend have friend access. They are accessible from within the program that contains their declaration and from anywhere else in the same assembly.

  • Protected Friend-Procedures declared Protected Friend have both protected and friend accessibility. They can be used by code in the same assembly, as well as by code in derived classes.

  • Private-Procedures declared Private have private access. They are accessible only within their declaration context, including from any nested procedures.

  • name-Name of the Sub procedure.

  • arglist-List of expressions (which can be single variables or simple values) representing arguments that are passed to the Sub procedure when it is called. Multiple arguments are separated by commas. Note that in VB .NET, if you supply an argument list, you must enclose it in parentheses.

  • statements-The block of statements to be executed within the Sub procedure.

Each argument in the arglist part has the following syntax and parts:

[ <attrlist> ] [ Optional ] [{ ByVal | ByRef }] [ ParamArray ] argname[( )]
 [ As argtype ] [ = defaultvalue ]

Here are the parts of the arglist:

  • attrlist-List of attributes that apply to this argument. Multiple attributes are separated by commas.

  • Optional-Specifies that this argument is not required when the procedure is called. Note that if you use this keyword, all following arguments in arglist must also be optional and be declared using the Optional keyword. Every optional argument declaration must supply a defaultvalue. Also, Optional cannot be used for any argument if you also use ParamArray.

  • ByVal-Specifies passing by value. In this case, the procedure cannot replace or reassign the underlying variable element in the calling code (unless the argument is a reference type). ByVal is the default in Visual Basic.

  • ByRef-Specifies passing by reference. In this case, the procedure can modify the underlying variable in the calling code the same way the calling code itself can.

  • ParamArray-Used as the last argument in arglist to indicate that the final argument is an optional array of elements of the specified type. The ParamArray keyword allows you to pass an arbitrary number of arguments to the procedure. A ParamArray argument is always passed ByVal.

  • argname-Name of the variable representing the argument.

  • argtype-This part is optional unless Option Strict is set to On, and holds the data type of the argument passed to the procedure. Can be Boolean, Byte, Char, Date, Decimal, Double, Integer, Long, Object, Short, Single, or String; or the name of an enumeration, structure, class, or interface.

  • defaultvalue-Required for Optional arguments. Any constant or constant expression that evaluates to the data type of the argument. Note that if the type is Object, or a class, interface, array, or structure, the default value can be only Nothing.

Each attribute in the attrlist part has the following syntax and parts:

<attrname [({ attrargs | attrinit })]>

Here are the parts of attrlist:

  • attrname-Name of the attribute.

  • attrargs-List of positional arguments for this attribute. Multiple arguments are separated by commas.

  • attrinit-List of field or property initializers for this attribute. Multiple initializers are separated by commas.

Tip 

When you use ByVal (the default in VB .NET), you pass a copy of a variable to a procedure; when you use ByRef, you pass a reference to the variable, and if you make changes to that reference, the original variable is changed.

You call a Sub procedure using the procedure name followed by the argument list. The Exit Sub keywords cause an immediate exit from a Sub procedure. Finally, End Sub ends the procedure definition. Here's an example where I'm passing a text string, "Hello from Visual Basic", to the DisplayMessage Sub procedure, which displays that message in a console application:

Module Module1
    Sub Main()
        DisplayMessage("Hello from Visual Basic")
    End Sub

    Sub DisplayMessage(ByVal strText As String)
        System.Console.WriteLine(strText)
    End Sub
End Module

VB.net Home | All VB.net Tutorials | Latest VB.net Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Steven Holzner
Changes in Controls from VB6 to VB.net
Throwing a Custom Exception in VB.net
Throwing an Exception in VB.net
Using Finally in VB.net
Using Multiple Catch Statements in VB.net
Exception Filtering in the Catch Block in VB.net
Raising an Exception Intentionally in VB.net
Getting an Exception's Number and Description in VB.net
Using On Error GoTo 0 in VB.net
Using Resume Next and Resume Line in VB.net
Understanding Scope in VB.net
Properties in VB.net
Procedure Delegates in VB.net
Preserving a Variable's Values between Procedure Calls in VB.net
Specifying Optional Arguments with default values in Procedures in VB.net

More Tutorials in VB.net
Scope in VB.net
Unstructured Exception Handling in VB.net
Structured Exception Handling in VB.net
Creating Sub Procedures in VB.net
Creating Functions in VB.net
Passing a Variable Number of Arguments to Procedures in VB.net
Specifying Optional Arguments with default values in Procedures in VB.net
Preserving a Variable's Values between Procedure Calls in VB.net
Procedure Delegates in VB.net
Properties in VB.net
Understanding Scope in VB.net
Using Resume Next and Resume Line in VB.net
Using On Error GoTo 0 in VB.net
Getting an Exception's Number and Description in VB.net
Raising an Exception Intentionally in VB.net

More Latest News
Most Viewed Articles (in VB.net )
How to export from DataGridView to excel using VB.net
Your first VB.NET Crystal Reports - A step by step guide
How to export from database to excel using VB.net
Chat Server in VB.net
Read Data from Excel using OLEDB in VB.NET 2005
Sub Procedures and Functions in VB.net
Chat client in VB.net
Using Resume Next and Resume Line in VB.net
A tutorial on Chat Server and Chat Client in VB.net
DataTable Class in VB.net
Send SMS using VB code
File Extensions Used in VB .NET
Arrays and Dynamic Arrays in VB.net
How To Connect To A MySql Database in VB.net
Client Socket Program sample in VB.net
Most Emailed Articles (in VB.net)
How to create an XML file in VB.net
Multi threaded Socket Programming in VB.net
A tutorial on Chat Server and Chat Client in VB.net
How To Connect To A MySql Database in VB.net
Name Value Collecion in VB.net
String Insert, index off & format in VB.net
The text writer in VB.net
Vb.net binary reader and writer
How to Open and Edit Cells in an Excel 2007 file in VB.NET
Format Excel 2007 Page in VB.NET
Using Excel 2007 Chart in VB.NET 2005
How to export from DataGridView to excel using VB.net
Your first VB.NET Crystal Reports - A step by step guide
How to send email using VB.NET code
Sending email using CDOSYS in VB.net