Inside Trial Method
In the above example, I have declared and used a constructor with a single parameter for
a structure. Instead if I had tried to use a default parameter-less parameter I would have
got an error. But the same is possible in the case of classes as shown by the default
parameter-less constructor, which initializes the classvar variable to 28.
Another point to note is that a variable called anothervar has been declared and initialized
within the class whereas the same cannot be done for members of a structure.
Namespaces are used in .Net to organize class libraries into a hierarchical structure and
reduce conflicts between various identifiers in a program. By helping organize classes,
namespaces help programmers manage their projects efficiently and in a meaningful way
that is understood by consumers of the class library. Namespaces enables reusable
components from different companies to be used in the same program without the worry
of ambiguity caused by multiple instances of the same identifier.
Namespaces provide a logical organization for programs to exist. Starting with a top-
level namespace, sub-namespaces are created to further categorize code, based upon its
In .Net, the base class library begins at the System namespace. There are several classes
at the System level such as Console, Exception etc. The namespace name gives a good
idea of the types of classes that are contained within the namespace. The fully qualified
name of a class is the class name prefixed with the namespace name. There are also
several nested namespaces within the System namespace such as System.Security,
System.IO, System.Data, System.Collections etc.
Reducing conflict is the greatest strength of namespaces. Class and method names often
collide when using multiple libraries. This risk increases as programs get larger and
include more third-party tools.
Boxing is the implicit conversion of a value type to a reference type or to any interface
type implemented by this value type. This is possible due to the principle of type system
unification where everything is an object.
When boxing occurs, the contents of value type are copied from the stack into the
memory allocated on the managed heap. The new reference type created contains a copy
of the value type and can be used by other types that expect an object reference. The
value contained in the value type and the created reference types are not associated in any
C# Image: C#.NET Code to Add HotSpot Annotation on Images
Protect sensitive information with powerful redaction and encryption annotation objects to provide powerful & profession imaging controls, PDF document, image convert password protected pdf files to word online; break password on pdf
way. If you change the original value type, the reference type is not affected. Boxing,
thus, enables everything to appear to be an object, thereby avoiding the overhead required
if everything actually were an object.
Dim n as Integer = 10
Dim obj as Object
obj = n
In the above code segment, a value-type variable n is declared and is assigned the value
10. The next statement declares an object-type variable obj. The last statement implicitly
performs boxing operation on the variable n.
UnBoxing is the explicit conversion from a reference type to a value type or from an
interface type to a value type that implements the interface.
When unboxing occurs, memory is copied from the managed heap to the stack. For an
unboxing conversion to a given value type to succeed at run time, the value of the source
argument must be a reference to an object that was previously created by boxing a value
of that value type otherwise an exception is thrown.
VB.Net does not support the ability to explicitly unbox values. It relies on the helper
functions in the Microsoft.VisualBasic.Helpers namespace to carry out unboxing. Since
these helper functions are considerably less efficient than C# support for explicit
unboxing. Thus it is recommended to avoid excessive use of variables of type Object.
Boxing and UnBoxing have performance implications. Every time a value type is boxed,
a new reference type is created and the value type is copied onto the managed heap.
Depending on the size of the value type and the number of times value types are boxed
and unboxed, the CLR can spend a lot of CPU cycles just doing these conversions.
It is recommended to perform boxing and unboxing in a scenario where you have to pass
a value parameter multiple times to a method that accepts a reference parameter. In such
a case, it is advantageous to box the value parameter once before passing it multiple times
to methods that accept reference methods.
Enumerations are types that inherit from System.Enum. The elements of an enumeration
are expressed in words rather than numbers, which makes it convenient for understanding
the meaning of the value being used. Enumerations symbolically represent a set of values
of one of the primitive integral types.
The type of the elements of an enumeration can be Byte, Short, Integer or Long. If no
type is specified explicitly, the default type is Integer.
Enum month As Byte
Jan = 2
Feb = 5
Mar = 10
In the above code segment, an enumeration type month is declared. The underlying type
of the elements has been specified as Byte. It has three elements viz: Jan, Feb and Mar.
These three elements have been assigned specific values. In case of an enumeration, if no
values are specified, the value of the first element corresponds to 0 and so on.
The runtime supports constructs called delegates, which enable late-bound operations
such as method invocation and callback procedures. With delegates, a program can
dynamically call different methods at runtime. They are type safe, secure, managed
objects that always point to a valid object and cannot corrupt the memory of another
object. The closest equivalent of a delegate in other languages is a function pointer, but
whereas a function pointer can only reference Shared functions, a delegate can reference
both Shared and instance methods. Delegates are Marshal by Value Objects.
The members of a delegate are the members inherited from class System.Delegate.
A delegate defines the signature and return type of a method. The resulting delegate can
reference any method with a matching signature. Each instance of a delegate can forward
a call to one or more methods that take those parameters and return the same type. Once a
method has been assigned to a delegate, it is called when the delegate is invoked.
Delegate Function calculation(ByVal a As Integer, ByVal b As Integer) As
Public Function add(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
add = num1 + num2
Dim calc_delegate As New calculation(AddressOf add)
Dim result As Integer
result = calc_delegate(50, 70)
Four steps are required to implement delegates viz.
The foremost step is to define the delegate. The definition of the delegate specifies
the method signature, return type of the method, access modifier and the delegate
name. The method signature specifies the order and type of each argument.
The definition of a delegate is indicated by the usage of the Delegate keyword. As
shown in the above code segment, the delegate name is calculation, it's access
modifier is public, it receives two integer arguments and returns an integer value.
Creating Delegate Method Handler(s)
The next step is to define the method(s) that will be associated with the delegate.
In the above code segment, a method named add is defined. This method must have
same method signature as that of the delegate, as shown in the above code segment.
Hooking up Delegates and Method Handlers
For a delegate method handler to be invoked, it must be assigned to a delegate object.
In the above code, the delegate object is calc_delegate and is hooked up to the
method handler add.
Invoking the method through the Delegate
The last step is to invoke the methods that are associated with the delegate. A
delegate method handler is invoked by making a method call on the delegate itself.
This causes the method handler to invoke with the assigned input parameters as if
they were invoked directly by the program, as shown in the above code.
5.3 Features of VB.NET
Option Explicit and Option Strict
Option Explicit and Option Strict are compiler options that can be globally assigned to a
project and are interpreted at compile time. Setting these options enables programmers to
resolve some of the errors (e.g. typological errors) at compile time and thus prevent
Option Explicit was a feature of VB 6.0 and it has been made a part of .NET environment
too. This option can be used only at the module level. When this option is turned on, it
forces explicit declaration of variables in that module. This option can be turned "On" or
"Off". When it is not specified, by default, it is set to "Off".
Syntax: Option Explicit [On / Off]
When it is set to "On", it checks for any undeclared variables in the module at compile
time. If any undeclared variable is found, it generates a compile time error since the
compiler would not recognize the type of the undeclared variable. When it is set to "On",
variables can be declared using Dim, Public, Private or ReDim statements. Setting this
option to "On" helps programmers do away with any typological errors in the code.
When it is set to "Off", all undeclared variables are considered to be of type Object.
It is preferable to set this option to "On".
Visual Basic language in general does not require explicit syntax to be used when
performing operations that might not be optimally efficient (e.g. late binding) or that
might fail at run time (e.g. narrowing conversions). This permissive semantics often
prevents detection of coding errors and also affects the performance of the application.
VB.NET enables a programmer to enforce strict semantics by setting this option to "On".
When used, this option should appear before any other code. This option can be set to
"On" or "Off". If this statement is not specified, by default, it is set to "Off".
Syntax: Option Strict [On / Off]
When it is set to "On", it disallows any narrowing conversions to occur without an
explicit cast operator, late binding and does not let the programmer omit "As" clause in
the declaration statement. Since setting it to "On" requires explicit conversion, it also
requires that the compiler be able to determine the type of each variable. Thus it is
implied that Option Strict also means Option Explicit.
Visual Basic .NET allows implicit conversions of any data type to any other data type.
However, data loss can occur if the value of one data type is converted to a data type with
less precision or a smaller capacity. Setting this option to "On" ensures compile-time
notification of these types of conversions so they may be avoided.
Explicit conversions happen faster than the implicit conversions performed by the
system. In case of implicit conversions, the system has to identify the types involved in
the conversion and then obtain the correct handler to perform the conversion. In case of
explicit conversions, processing time gets reduced since the type of conversion is
From programmer point of view, explicit conversion may seem to be a burden since it
slows the development of a program by forcing the programmer to explicitly define each
conversion that needs to occur.
It is always recommended that you set Option Strict to "On" and use explicit conversions.
ByVal is Default
When implementing functions/procedures we often need to pass information. This
information can be passed to the called function/procedure through parameters.
Parameters can be passed by value or by reference.
When ByVal keyword is used, it causes the parameter to be passed by value. In this case,
a copy of the original parameter is passed to the called module. Thus any changes made
to the copy of the parameter do not affect the original value of the parameter.
When ByRef keyword is used, it sends a reference (pointer) to the original value to the
called module rather than its copy. Thus any changes made to the parameter in the
function/procedure will cause the original value to be modified.
With ByRef you are exposing a variable to modification which can lead to an unexpected
behavior. If that procedure calls another procedure and passes the same parameter ByRef,
the chances of unintentionally changing the original variable are increased.
In VB.NET, every parameter, by default, is passed by value if nothing is explicitly
specified. Thus it protects arguments against modification.
Public Class SampleClass
Sub Proc(ByVal a As Integer, b As Integer, ByRef c As Integer)
a = a + 2
b = b + 3
Documents you may be interested
Documents you may be interested