Data Types and Access Specifiers in VB .Net
VB .Net Data Types
- VB .Net has a wide variety of data types which are categorized into the following groups: –
(A) Numeric Data Types
Data Type | Memory Size Consumed (in Bytes) | Default Value | Data Value Range | Name Space Required | |
---|---|---|---|---|---|
Short | 2 | 0 (Small Integer) | 16-bit signed integer | System.Int16 | |
Integer | 4 | 0 (Medium Integer) | 32-bit signed integer | System.Int32 | |
Long | 8 | 0 (Long Integer) | 64-bit signed integer | System.Int64 | |
Single | 4 | 0.0 (Small Float) | 32-bit floating point variable | System.Single | |
Double | 8 | 0.0 (Medium Float) | 64-bit floating point variable | System.Double | |
Decimal | 16 | 0.0 (Long Float) | 128-bit floating point variable | System.Decimal |
(B) Character and String Data Type [Non-Numeric Data Type]
Data Type | Memory Size Consumed (in Bytes) |
Default Value | Value Range | Name Space Required | |
Char | 2 |
Stores only a single character enclosed between double quotation marks. |
System.Char | ||
String | Varies (Up to 2 billion characters) |
stores textual information as letters, digits, and some special characters/combinations of all these and is enclosed between double quotation marks. |
System.String |
(C) Byte Data Type
Data Type | Memory Occupied (Bytes) | Default Value | Value Range | Name Space Required |
Byte | 1 | 0 (Byte data type is used to store binary data such as binary files, images, sounds, etc. It is an unsigned Byte data type) | System.Byte | |
SByte | 1 | 0 (stores binary data such as a binary file, image, sound, etc…It is a signed Byte data type. ) | — |
(D) Boolean Data Type
Data Type | Memory Occupied (Bytes) | Default Value | Value Range | Name Space Required | |
Boolean |
2 (stores true or false value. We can also assign an integer value to a Boolean variable. Any Non-Zero value is considered TRUE and zero is FALSE.) |
FALSE |
stores true or false value. we can also assign integer values to Boolean variables. Any Non-Zero value is considered as TRUE and zero is considered as FALSE. |
System.Boolean |
(E) Date Data Type
Data Type | Memory Occupied (Bytes) | Default Value | Value Range | Name Space Required | |
Date | 8 |
Date datatype is used to store Date and Time values. |
System.Date |
(F) Object Data Type
DataType | Memory Occupied (Bytes) | Default Values | Value Ranges | Name Space Required | |
Object | 8 |
It is the default type of the variable. It means if you don’t declare the variable then the default Data Type for that variable is object. |
stores the value of any data type. | System.Object |
VB .Net Access Specifiers/Modifiers
Introduction
- Access specifiers are an integral part of object-oriented programming.
Definition
- Access specifiers define how the object can access a class’s members (data or its attributes and associated methods).
- Access Specifiers are the way the scope of accessibility of an object and its associated data members and methods present in a VB .net program.
- In VB.NET, access specifiers (also known as access modifiers) are keywords used to define the visibility and accessibility of classes, methods, variables, and other members within a class or module.
Features
- The access modifiers are keywords that are placed before their associated type.
- These specifiers control where and how the members can be accessed, which is crucial for encapsulation, security, and the overall design of the application.
Advantages
- We can control the scope of the data members & methods object of a class using proper access specifiers as per need of the program requirements.
- Access specifiers provide security for our application’s/class data as well.
Types
- Visual Basic .Net has five typical access specifiers which are specified as follows :
- Public
- Private
- Protected
- Friend
- ProtectedFriend
Public :
-
- The public is the most common & least secure access specifier.
- In this, the data members & methods of a class can be accessed by the objects from anywhere either inside or outside the program/class i.e., there is no restriction at all on accessibility of the object of same or different class.
- The Public access specifier allows a class, method, or variable to be accessible from any other code in the same assembly/class or another assembly/class that references it.
- It’s typically used for members that need to be accessible across different parts of an application, or from other applications or libraries.
- Public access specifier is used only at module, interface, or namespace level i.e., we can declare a public element at the level of a source file or namespace, or inside an interface, module, class, or structure, but not in a procedure.
- For example –
Protected :
-
- The scope of accessibility of protected access specifier is limited and within the same class and the classes derived (through inheritance) from this class.
- Protected members are accessible within the class where they are declared, as well as its derived classes. However, they cannot be accessed from outside the class hierarchy.
- We can use a protected access specifier only at the class level, and only when we declare a member of a class i.e. we can declare a protected element in a class, but not at the level of a source file or namespace, or inside an interface, module, structure, or procedure.
- For example –
Friend :
-
- In VB.NET, the default access specifier for a class member (field, property, method, etc.) is “Friend”.
- If we want to allow a class to be accessed from outside the assembly, we can use the “Public” access specifier for the class in place of a friend.
- The Friend access specifier allows access to the members within the same assembly, but not from another assembly i.e., the Friend access specifier can access the elements within the program that contain its declarations and also access within the same assembly level, but not from outside the assembly.
- We can also use the friend keyword instead of the Dim keyword.
- We can use the friend access specifier only at the module, interface, or namespace level. This means that we can declare a friend element at the level of a source file or namespace, or inside an interface, module, class, or structure, but not in a procedure.
- For example –
ProtectedFriend :
-
- It is a combination of features of both protected and friend i.e. it can be accessible from anywhere in the same assembly(friend) and in the same class also the classes inherited from the same class.
- The element can be accessed either from derived classes or from within the same assembly, or both.
- This specifier is used when we want to share access with derived classes, but still want to restrict the visibility to the containing assembly.
- We can use ProtectedFriend only at class level, and only when we declare a member of a class. This means that we can declare a protected friend element in a class, but not at the level of a source file or namespace, or inside an interface, module, structure, or procedure.
- For example –
Private Protected (Available in VB.NET 15.5 and later):
-
- The ‘Private Protected’ access specifier restricts access to members of the containing class and any derived classes that are within the same assembly.
- This specifier is more restrictive than ‘Protected Friend’ and is used when we want to allow access in derived classes but only within the same assembly.
- For example –
Private :
-
- They are the most secure access specifier and help to protect the internal state and behavior.
- It is the least permissive access level.
- The scope of the accessibility of Private access specifier is restricted i.e., only for those objects which are declared inside the same class or module. The element can be accessed only from within the same module, class, or structure.
- In other words, members declared as private are only accessible within the class or module where they are declared. They cannot be accessed from outside the class or module or even from derived classes.
- The Private members (data and methods) cannot be accessed by the object outside the class.
- We can use private only at the module level. This means that we can declare a private element inside a module, class, or structure, but not at the level of a source file or namespace, inside an interface, or in a procedure.
- At the module level, the Dim statement without any access level keywords is equivalent to a private declaration. However, we might want to use the private keyword to make the code easier to read and interpret.
- For example –
Type Conversion
- There are two ways to perform data type conversions in VB. NET:-
- Narrowing conversion:
- This is done by the compiler automatically.
- This is the easiest and most common way of data conversion.
- For example –
- Narrowing conversion:
-
- Implicit conversion:
- The VB .Net compiler allows this conversion to take place when the Option Strict switch is off, otherwise may arise error. This switch tells the compiler whether or not to perform strict type checking otherwise a design-time error is displayed. This switch is set on or off by putting it at the beginning of our program.
- For example –
- Implicit conversion:
0 Comments