Creating Sub Procedures in VB.net
By: Steven Holzner in VB.net Tutorials on 2010-11-17
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
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Changes in Controls from VB6 to VB.net
Unstructured Exception Handling in VB.net
Structured Exception Handling in VB.net
Creating Sub Procedures 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
Throwing an Exception in VB.net
Comments