VARIABLE AND CONST IN VB.NET A variable is used to store value that can be used in the future in the programming. Variables can be created in order to storing different type of data as they are numbers, characters or combination of anything. When we say variable that it is always a namespace in the memory or simply says a space allocated to store data in a namespace in the memory which can be created with letters only, letters and numbers, or letters, numbers and underscore. In This Tutorial:
Understanding about Variables in VB.NETActually, this is a first question: What is a Variable? A variable is a namespace which is used to store and retrieve the data where needed in the programming. Why is a variable in VB.NET? When a programmer needed something to store the data for temporarily or permanently in the purpose of reuse, variable will be the best option to do that. And most importantly variable can be used as predefined to store the data at the later time or preassigned as it is being assigned the data when it's created. How to create a variable in VB.NET? When you want to create a variable should be followed the syntax of variable declaration. Syntax: Dim [variable_name] As Datatype [Value (Optional)] The declaration of a variable is simple that requires a variable name and data type followed by a Dim. A Dim is used in Class, Module, structure, Sub and procedure. A valid declaration: Dim First_Name As String Dim Num_01 As Integer = 22 An invalid variable declaration: Click here "VB.NET KEYWORDS" and read for further understanding. Assigning more than one variable with one Dim: When you want to create many variables with the same datatype use the comma at the end of each variable. And it's important that you should understand about all the variables will be the same datatype and can be used for store only the particular datatype(data) as if you've chosen as Integer, then you can't use that variable for storing the first name or last name and only can be used to store numbers. Syntax: Dim [Variable_Name1], [Variable_Name2], [Variable_Name3] As Integer A valid declaration: Dim first_name, middle_name, lastname As String After a declaration of a variable, how to assign a value/data to it? Example: Dim first_name As String Dim Age as Integer first_name = "Mano" Age = 22 The above data assigns are valid. Const in VB.NET The name constant refers to a fixed value that cannot be changed during the execution of a program. It is also known as literals. These constants can be of any data type, such as Integer, Double, String, Decimal, Single, character etc. Declaration of Const: The const is a keyword in VB.NET that is used to declare a variable as constant. The Const statement can be used with module, structure, procedure, form and class. When you want to declare a variable with const should be followed the syntax of const variable declaration. Syntax: Const const_name As Datatype = Value How to declare variables with different Const Structurals?
Examples:
Scope of Variable in VB.NET The scope of a variable determines the accessible range of a defined variable at the time of declaration in any block, module, and class. You can access it if the variable is in a particular region or scope in the same block. And if the variable goes beyond the region, its scope expires. Following are the methods to represent the scope of a variable in VB.NET.
Global (Public) Scope The global variable is a variable that is used to access the variables globally in a program. It means these variables can be accessed by all the procedures or modules available in a program. To access the variables globally in a program, you need to use the friend or public keyword with a variable in a module or class at the top of the first procedure function. Global scope is also known as the Namespace scope. Example: Public string_hw As String = "Hello World" Procedure (local) scope A local variable is a type of variable defined within a procedure scope, block, or function. It is available with a code inside the procedure, and it can be declared using the Dim or static statement. These variables are not accessible from outside of the local method. However, the local variable can be easily accessed by the nested programming function in the same method.
The local variables exist until the procedure in which they are declared is executed. Once a procedure is executed, the values of its local variables will be lost, and the resources used by these variables will be released. And when the block is executed again, all the local variables are rearranged. Module Scope The existing procedures can easily identify a variable that is declared inside a module sheet is called a module-level variable. The defined module variable is visible to all procedures within that module only, but it is not available for other module's procedures. The Dim or private statement at the top of the first procedure declaration can be declared the module-level variables. It means that these variables cannot be declared inside any procedure block. Further, these variables are useful to share information between the procedures in the same module. And one more thing about the module-level variable is that these variables can remains existence as long as the module is executed. Below is the declaration section of the module:
0 Comments
|
Archives |