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 TypeMemory Size Consumed (in Bytes)Default ValueData Value RangeName Space Required

Short

2

 0 (Small Integer)

16-bit signed integerSystem.Int16

Integer

4

0 (Medium Integer)

32-bit signed integerSystem.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 variableSystem.Double

Decimal

16

0.0 (Long Float)

128-bit floating point variableSystem.Decimal
 
(B) Character and String Data Type : [Non-Numeric Data Type]
Data TypeMemory Size
Consumed  (in Bytes)
Default ValueValue RangeName Space Required

Char

2

stores only single character enclosed between double quotation mark.

System.Char

String

Varies (Up to 2 billion characters)

stores textual information as letters, digit and some special characters/combination of all these and  enclosed between double quotation mark.

System.String

(C) Byte Data Type :
Data TypeMemory Occupied (Bytes)Default ValueValue RangeName Space Required

Byte

1

0 (Byte data type is used to store binary data such as binary file, image, sound etc. It is unsigned Byte data type)

System.Byte

SByte

1

0 (stores binary data such as binary file, image, sound etc…It is signed Byte data type. )

(D) Boolean Data Type
Data TypeMemory Occupied (Bytes)Default ValueValue RangeName Space Required

Boolean

2 (stores true or false value.  we can also assign integer value to Boolean variable. Any Non-Zero value is considered as TRUE and zero is considered as FALSE.)

FALSE

stores true or false value.  we can also assign integer value to Boolean variable. Any Non-Zero value is considered as TRUE and zero is considered as FALSE.

System.Boolean

(E) Date Data Type
Data TypeMemory Occupied (Bytes)Default ValueValue RangeName Space Required

Date

8

Date datatype is used to store Date and Time value.
You can assign values to Date variable using one of the following methods:
CurrentDate = Now.Date          ‘assign current system date
CurrentDate = #08/03/1982#    ‘assign user specific date

System.Date

(F) Object Data Type
DataTypeMemory Occupied (Bytes)Default ValuesValue RangesName 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 value of any data type.

System.Object

VB .Net Access Specifiers/Modifiers

Introduction

Definition

  • Access specifiers defines how the members (data or its attributes and associated methods) of a class can be accessed by the object.
  • Access Specifiers are the way of scope of accessibility of an object and its associated data members and methods present in a VB .net program.

Features

  • The access modifiers are keywords that are placed before its associated type.

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 provides security of 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 :
      • 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. 
      • 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.
      Protected :
        • The scope of accessibility of protected access specifier is limited and within the same class and the classes derived (Inherited) from this class.
        • Protected members are accessible within the class where they are declared, as well as in any derived classes. However, they cannot be accessed from outside the class hierarchy.
        • We can use protected access specifier only at 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.
      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 friend.
        • 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 friend keyword instead of Dim keyword.
        • It is not commonly used in VB .NET.
        • We can use friend access specifier only at 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.
      ProtectedFriend :
        • It is combination features of both protected and friend i.e. it can be accessible from anywhere in the same assembly 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.
        • 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.
      Private :
        • They are considered as the most secure access specifier.
        • The scope of the accessibility of this access specifier is restricted i.e., only for those objects which are declared inside the class.
        • Members declared as private are only accessible within the class where they are declared. They cannot be accessed from outside the class or even from derived classes.
        • The element can be accessed only from within the same module, class, or structure.
        • We can use private only at 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. 
        • The Private members (data and methods) cannot be accessed by the object of outside the class.
        • It is the least permissive access level.

      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 –
      Dim x As Single = 3.14159
      Dim y As Integer = x
      Console.WriteLine(y)         ‘ Output: 3
        • 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 at the beginning of our program.
          • For example –
      Option Strict On
      Imports System
      Module Module1
      Sub Main()
      Dim x As Integer= 42
      Dim y As Single= x
      Console.WriteLine(y)         ‘ Output: 42.0
      End Sub
      End Module

      0 Comments

      Leave a Reply

      Your email address will not be published. Required fields are marked *

      This site uses Akismet to reduce spam. Learn how your comment data is processed.