Error Message: Object cannot be cast from DBNull to other types.
Solution: This exception occurred because the data type of data column of data row accepts null values, but the data type in C# not. All the data type that are "value types" (struct) don´t accepts null values.
Thursday, April 19, 2012
Object cannot be cast from DBNull to other types. How to resolve this Exception Message?
Sunday, April 15, 2012
Differences between value type and reference type?
| Value Type | Reference Type |
| Value type variable directly contain value itself. | Reference type variable contain the address of object where the values are placed in memory. Or we can say they store the reference to object. |
| All value types are implicitly derived from System.ValueType. | They are derived from System.Object class. |
| They doesn’t support inheritance. Means they are sealed implicitly. | They can be inherited. They are internal by default. |
| A value type variable can’t be assigned value null. | An object can have value null. |
| They can be declared without new operator. | They can’t be declared without new operator. |
Differentiate between Structure and Class in .net?
| Structure | Class |
| Structures are value type, i.e. variable of struct type directly contain the data of structure | Classes are reference type, i.e variable of class (known as object) contain the reference to data. |
| Do not require, urgently, new operator to do its instantiation. Or instantiation can be done without new operator. | Do require new operator to do instantiation. |
| Each variable will have its own set of data even if they have same values. | More than one object can have reference to same data. |
| Default Value of a struct type can’t be null. | Default value of an Object of a class type is null. |
Thursday, March 22, 2012
What is a Constructor in C#?
A constructor is a member function that has the same name as the class name and is invoked automatically when the class is instantiated. A constructor is used to provide initialization code that gets executed every time the class is instantiated.
Points to be noted on constructors:
- Constructors cannot be "virtual".
- They cannot be inherited.
- Constructors are called in the order of inheritance.
- If we don't write any constructor for a class, C# provides an implicit default constructor, i.e., a constructor with no argument.
- Constructors cannot return any value.
- Constructors can be overloaded.
The syntax for a constructor is as follows:
[modifier] name (parameters)
{
// constructor body
}
Monday, March 19, 2012
Abstract class and Interface
Introduction
In this article along with the demo project I will discuss Interfaces versus Abstract classes. The concept of Abstract classes and Interfaces is a bit confusing for beginners of Object Oriented programming. Therefore, I am trying to discuss the theoretical aspects of both the concepts and compare their usage. And finally I will demonstrate how to use them with C#.
Background
An Abstract class without any implementation just looks like an Interface; however there are lot of differences than similarities between an Abstract class and an Interface. Let's explain both concepts and compare their similarities and differences.
What is an Abstract Class?
An abstract class is a special kind of class that cannot be instantiated. So the question is why we need a class that cannot be instantiated? An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.