Thursday, May 29, 2008

Structures in C#

Structures:

 

Points:

1. Its a value type so if we assign a value to another struct then it is stored at two different point in the stack.

2. We cannot have a explicit empty constructor in a structure.

3. Structure cannot have constructors that do not completely initialize all the variables in it.

4. There is a 16 byte size limit to structure's instance. All value types are 16 bytes or shorter.

5. It is logically a single value.

6. It will not be cast to a reference type.

7. If a copy of a structure is passed to another procedure or function then the original will not change after the call. This is because a copy is passed to that procedure or function.

8. Value types are far more efficient then reference types however there is a instance size limit.

Strategy Pattern Part-2.

Tried to create the strategy pattern example as a vehicle that can fly or a drive on the road.
Intreastingly the go method that is implemented in each of the vehicle sub classes is not hard coded into the subclass itself.
That means that a car go method is not very different from a helicopter go method. And here is the kicker: The method is not implemented in the car and vehicle classes.

The go method is instead created in an interface.
So whats the big idea? We could as well create an interface and then implement it there.

ah but the interface is implemented in two classes GoDriving class and GoFlying class.

The abstract Vehicle class has a method that takes in an interface and decides what function to call. And then takes that interface and decides what go method to call.

Its easy to understand because an interface in the Vehicle class is replced by the actual class(goFlying or goDriving is used) that implements that interface in the car class. Then the go method of

And all that is there in the car class is a call to GoDriving class to intantiate a variable.
The helicopter class therefore has only a call to GoFlying. This ensures that the go method can be modified without modifying the Car and Helicopter classes.

The advantage becomes apparent when there is a huge number of classes and they may keep changing over time.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{

public interface goInterface
{
void goMethod();

}

class goDriving : goInterface
{

#region goInterface Members

void goInterface.goMethod()
{
System.Console.WriteLine("Go Driving");
}

#endregion
}

class goFlying : goInterface
{
#region goInterface Members

public void goMethod()
{
System.Console.WriteLine("Go Flying");
}

#endregion
}

public abstract class Vehicle : goInterface
{
public goInterface travelMode;
public void setTravelMode(goInterface x)
{
travelMode = x;
}
public void goMethod()
{
travelMode.goMethod();
}


}

public class Car : Vehicle
{
public Car()
{
goDriving drx = new goDriving();
setTravelMode(drx);
}


}

public class AeroPlane : Vehicle
{


}



class Program
{
static void Main(string[] args)
{
Car x = new Car();
x.goMethod();
System.Console.ReadLine();
}

}
}

Why use design patterns

1. they are a quick and easy way to benefit from the knowledge of others.
2. There is no point in reinventing the wheel.

Have been reading some books to increase my awareness of the subject.
Design Pattern in C# by Steven John
and Design Patterns for dummies.
The dummies book is in java so that is fine.

Strategy Design Pattern

Strategy Design Pattern:
Have been reading Strategy design pattern from various books including Design Patterns in C# and Design Pattern for dummies.

Use when there is a section of code that will change frequently.
1. Instead of modelling is a relationship

like Class Vehicle
then Class car:Vehicle
        Class plane:Vehicle,

        We do class Vehicle:IDriving;
                so car:Vehicle, IDriving
                and plane:Vehicle, IDriving
           which is not so good because if something changes then it has
           to be changed in each class file.


                               so

1. Create a n interface that the subclasses will use
        in this case

 public Interface Igo{
                        public void  go();
                                              
                        }


2  public class goDriving: Igo
  {

   go(){
   System.Console.Writeline("Driving");
   }

  }
3.
  public class goFlying: Igo
  {

   go(){
   System.Console.Writeline("Flying");
   }

  }


4.
  public abstract class Vehicle
  {
     private Igo goMethod;
          
  }


Database Access in ADO.NET

Database Access in ADO.NET

Data providers:  Set of classes that allows us to
access a particular database, execute commands and
retrieve/update data.

Data providers generally have the following classes
1. Connection   : use to connect
2. Command      : used to execute commands or stored procs
3. DataReader   :Object that provides read only,forward
only access
4. DataAdapter  :Object that is used to populate the
dataset(disconnected tables). Or used to apply changes
to  a database.

There are four providers
1. SQL Server Provider  : Access to sql server
2. OLE DB Provider      :Access to data sources which
provide ole db drivers
3. Oracle provider              : Optimized access to Oracle
database.
4. ODBC provider                :access to an odbc data source.


Try Catch Throw

Throw Catch Exceptions
1. Try the block
2. Catch the exception.
3. Cleanup any remaining issues within the finally section.

Filtering Exceptions: We can use multiple catch for a single try section.
Only the first catch will be executed and then it will switch to finally.

The finally section cannot access variables declare in the try section.

If we want to create a variable declaration and make sure that it is cleaned
up even if an error occurs, then use the "using" keyword.

Tuesday, May 13, 2008

Asp.Net

Say wonder what is the need of the reusable property in i http
handler? It enables sharing of the instance with other requests so
what? Whats the advantage in using that?

--
Sent from Gmail for mobile | mobile.google.com