Documente Academic
Documente Profesional
Documente Cultură
NET
Everything in the world is an object. The type of the object may vary. In OOPS, we get the power to create
objects of our own, as & when required.
EMPLOYEE
Using inheritance, we may assign different traits to different classes. Yet, the child classes will inherit
some common traits from the base class. As in the figure above, the classes for "Permanent" and "Wage
Worker" will inherit some common traits from the class of "Employee".
A class may contain class members like fields, properties, events & methods with different
types of access modifiers like private, public, protected or friend, to process the way they are
accessed. In VB.NET, a class may be declared as below.
End Class
Key Concepts of .NET - To work with classes and modules, the key concepts to know are
definition, access, inheritance, constructors, destructors, delegates, abstract classes &
interfaces.
Class members - The different types of entities in a class, like fields, properties, events &
methods.
Object - An object is an instance of a class. This instance may be used to access the members
of the class. It is pretty easy to define an object. See sample code below.
Structure - It is a bit similar to a class. Semantically, structures are known as value types,
while classes as reference types. We don't instantiate an object using the New keyword while
working with a structure. We can not inherit from a structure.
End Structure
Dim objStud as Student
objStud.RollNo=31
objStud.Name="Sandy"
Here, note that the object objStud is not exactly an instance, it is a simple object (object of a structure)
that is used to access the members of this structure. It would have behaved differently had it been an
object of a class, as it would have invoked the constructor associated with the class.
End Function
End Class
Now, lets make use of the method FnAdd defined in the class above. To use it, first create an object of this
class, say objX. Using this object, we can invoke the methods of this class. See code below.
dblResult = objX.FnAdd(4.56,2.35)
Property - A property is a thing that describes the features of an object. A property is a piece
of data contained within a class that has an exposed interface for reading/writing. Looking at
that definition, you might think you could declare a public variable in a class and call it a
property. While this assumption is somewhat valid, the true technical term for a public variable
in a class is a field. The key difference between a field and a property is in the inclusion of an
interface.
We make use of Get and Set keywords while working with properties. We prefix the variables used within
this code block with an underscore. Value is a keyword, that holds the value which is being retrieved or
set.
Get
Return _Color
End Get
Set(ByVal Value)
_Color = Value
End Set
End Property
Event - An action that an object does. When something happens, we say an event has
happened. For example, when a button is clicked, we say it is the click( ) event. When a
mouse hovers on an image, we say the mouseover( ) event has taken place.
Access Modifiers - Keywords used to vary the way members of a class are used. Following are
different types.
2) Private - These classes are accessible only within their declaration contexts. This includes
nested procedures. When a variable is declared Public inside a Private class, it is accessible
only from inside that class.
3) Protected - These classes extend the accessibility of their members to their child classes
(means, to the classes that derive from them). They extend their members to both themselves
& their child classes.
4) Friend - Friend access means that elements are accessible only within the program. Friend
is the default access modifier for any class that does not have a modifier.
5) Protected Friend - Available only to classes that inherit from our class (in any project) or to
code within our project/component. This is a combination of Protected and Friend.
Default - A Default property is a single property of a class that can be set as the default. This
allows developers that use your class to work more easily with your default property because
they do not need to make a direct reference to the property. Default properties cannot be
initialized as Shared or Private and all must be accepted at least on argument or parameter.
Default properties do not promote good code readability, so use this option sparingly.
Overloads - The Overloads property allows a function to be described using deferent
combinations of parameters. Each combination is considered a signature, thereby uniquely
defining an instance of the method being defined. You can define a function with multiple
signatures without using the keyword Overloads, but if you use the Overloads keyword in one,
you must use it in all of the function's Overloaded signatures.
Shared -The Shared keyword is used in an inherited or base class to define a property or
method as being shared among all instances of a given class. If multiple instances of a class
with shared properties or methods are loaded, the shared properties or methods will provide
the same data across each instance of the class. When one class alters the value for a shared
property, all instances of that class will reflect the change. Shared properties of all instances of
the class point to the same memory location.
Overides - The Overrides keyword allows the inheriting class to disregard the property or
method of the inherited class and implements ts own code.
MustOverride - The MustOverride keyword forces the inheriting class to implement its own
code for the property or method.
Shadows - The Shadows keyword will effectively hide all of the other methods in the baseclass.
It is like forcefully getting rid of the overloading that has been done on the methods of the
base class. The Shadows keyword works like the Overloads keyword except that with shadows
we do not have to follow rules such as implementing the same signature. The Shadows
keyword does not require the consent (override ability) of the inherited class to replace the
property or method's implementation code. A method does not have to be defined as
overridable for the Shadows keyword to work. Read the example...
End Sub
End Sub
End Class
'This is the Child Class
Inherits Parent
End Sub
End Sub
End Class
c.MyProc("Hello Wazzup!")
c.MyProc(12)
c.MyProc(Chr(66))
Inherits Parent
End Class
Constructor - When a class instance is created in our code, a special method of that class,
called the constructor, is called. Similarly, when the class is destroyed, the destructor method
is called. These are general terms and usually not the actual member names in most object-
oriented languages. It is initialized using the keyword New, and is destroyed using the
keyword Finalize. In .NET, we tend to forget using Finalize as the instances(means the object)
are automatically destroyed by the Garbage Collector, when the object is not in use by he
CLR(Common Language Runtime).
objsampleobject.Finalize
We can add parameters to the constructors. This wasn’t allowed in VB6. We can overload the constructors,
change the order of parameters, data types of parameters which ultimately change the way the
constructor works whenever an instance of that class is invoked.
Also note that a constructor can have any access modifier. If no argument is added in a constructor,
VB.NET adds a no-argument constructor during compile time. It adds the Public Sub New( ) with the class
declared, if no argument is passed in the constructor. The following code is added...
End Sub
However, when the constructor is added with parameters, the following code is generated...
End Sub
When a child class' object is declared, the child class constructor & its parent class constructor, both are
invoked. Read example below for more clarity...
System.Console.WriteLine("Book's constructor.")
End Sub
System.Console.WriteLine("This is a book.")
End Sub
End Class
System.Console.WriteLine("Author's constructor.")
End Sub
End Class
When the Authors class' constructor is invoked, like in the following code, the Books class' no-argument
constructor is also called.
Book's constructor.
Author's constructor.
If the base class doesn’t have a no-argument constructor, then it would result in a compiler error. Hence,
we need to use the MyBase keyword with the constructor. Our child class will look like this...
MyBase.New(SomeString)
System.Console.WriteLine("Authors's constructor.")
End Sub
End Class
If a class is not inheriting from any base class, then it will call the base class constructor of System.Object
if we are using MyBase.New( ). Summarizing constructors, whenever we initiate a constructor, the
following things happen...
Base class constructor is invoked.
If the argument name passed in the constructor, is same as the variable name used in the constructor, we
use the Me keyword to refer to the constructor variable. For example if the variable name is SomeString,
and the parameter passed is also SomeString, then the variable is referred as Me.SomeString.
Abstract Class - They are classes that cannot be instantiated. We cannot create an object from
such a class for use in our program. We can use an abstract class as a base class, creating
new classes that will inherit from it. Creating an abstract class with a certain minimum
required level of functionality gives us a defined starting point from which we can derive non-
abstract classes.
An abstract class may contain abstract methods & non-abstract methods. When a class is derived from an
abstract class, the derived class must implement all the abstract methods declared in the base class. We
may use accessibility modifiers in an abstract class (unlike in Interfaces).
An abstract class can inherit from a non-abstract class. In C++, this concept is known as pure virtual
method.
Interface - its a kind of class, that has only methods, do not have code, just the definition of
the methods. Also, the interface can't be instantiated. Its an abstract class with public abstract
methods, all of which must be implemented in the inherited classes. All methods in an
interface are public, no other access modifier is used. It is public by default.
Classes can contain code, but interface don’t. However, classes that implement an interface do contain
code. Keep in mind that there are no instances of interfaces in VB .NET. Every instance is a type that
implements an interface, but is itself not an instance of the interface. Also note, in an interface, all
methods must be abstract (which is not necessary in an abstract class).
End Interface
Implements ISalary
Public Function Salary() As Decimal Implements ISalary.CreditSalary()
End Function
End Class
Serializable - This is a class attribute. When we use this attribute with a class, an instance of
this class can be taken in whatever state it is, and write it to a disk. The class can then be
desterilized, and the class will act as if it is simply stored in the memory.
Boxing & Unboxing - Value Types are stored on the stack and Reference types are stored on
the heap. The conversion of value type to reference type is known as Boxing. Converting
reference type back to value type is known as Unboxing.
Value Types - Value types are primitive types that are mapped directly to the FCL. Like Int32
maps to System.Int32, double maps to System.double.
All value types are stored on stack and all the value types are derived from System.ValueType. All
structures and enumerated types that are derived from System.ValueType are created on stack, hence
known as ValueType.
Reference Types - Reference Types are different from value types in such a way that memory
is allocated to them from the heap. All the classes are of reference type. C# new operator
returns the memory address of the object.
Partial Class - This concept has been introduced in .NET framework 2.0. They give you the
ability to split a single class into more than one source code (.cs or .vb) file. Here's what a
partial class looks like when it's split over two files...
public MethodA()
{...}
public MethodB()
{...}
}
When you build the application, Visual Studio .NET tracks down each piece of MyClass and assembles it
into a complete, compiled class with two methods, MethodA() and MethodB().
Partial classes don't offer much in the way of solving programming problems, but they can be useful if you
have extremely large, unwieldy classes. (Of course, this might be a sign that you haven't properly factored
your problem, in which case you should really break your class down into separate classes.) The real
purpose of partial classes in .NET is to hide automatically generated designer code.