Exception in System.Drawing.dll

There is a small problem with System.Drawing.dll in VS.NET 2003. Sometimes you get an Arithmetic Exception and the program is terminated. If u get this "Arithmetic Exception" in System.Drawing.dll, then there must be some spy software or some virus installed on your system which has hooked the system and changed the FPU (FloatingPoint Unit value) for all the processes.
 
To Set the FPU value to dafault u can use _controlfp() function defined in "msvcrt.dll". This is a native function, to call this function in C# u will have to use .NET Runtime Interop Services. To do this add the following lines of code to your Form class
 
[DllImport("msvcrt.dll")]
private static extern int _controlfp(int n, int mask);
private const int _MCW_EW = 0x8001F;
private const int _EM_INVALID = 0x10;
 

Now call this function at the start of your form constructor

_controlfp(_MCW_EW, _EM_INVALID);
 
The FPU value is now set to default and you will not get any Exception in system.Drawing.dll 
 
 

Validating numeric data type

Many times we face the situation where we want to validate whether user input is numeric or not. In .Net it is reallly esy to validate the data type of user input using Regular Expressions. An example of how to validate that the data type of user input is numeric is:
 

bool Validate(string text)

{

Regex reg=new Regex(@"\D");

return !reg.Match(text).Success;

}

 

Here if text contains only digits then the result of reg.Match is false, so Validate will return true and vice versa. Here decimal point is also considered as non digit and will cause the result of the function to be false.

 

Now the following code snippet will recognize the period "." as the part of the digits also but more than one periods will not cause error i.e 123.45.67 will also return true.

 

bool Validate(string text)

{

Regex reg=

new Regex(@"[^0-9.]");

return !reg.Match(text).Success;

}

How to create a VB Script Object and call methods on it

Well you will be surprised to know that it is very easy to create a VB Script object in C# and call methods on it. Consider the following VB Script Code:
 
Dim IIsWebServiceObj
Set IIsWebServiceObj = GetObject("IIS://localhost/W3SVC")
 
IIsWebServiceObj.DeleteExtensionFileRecord "E:\RMS\Code\BusinessServices\KalSPS\Debug\KalSPS.dll"
 
To write this code in C#, you just need to add a reference to Microsoft.VisualBasic.CompilerServices and System.Runtime.CompilerServices. The following code snippet creates an IIS object and call DeleteExtensionRecord Method on it.
 

private

void RemovesWebServiceExtension(string extensionPath)

{

try

{

object ext = this.GetIISObject("IIS://" + Environment.MachineName+"/W3SVC");

object[] args=new object[1]{extensionPath};

LateBinding.LateCall(ext, null, "DeleteExtensionFileRecord", args, null, null);

}

catch

{

throw;

}

}

private object GetIISObject(string fullObjectPath)

{

try

{

return RuntimeHelpers.GetObjectValue(Interaction.GetObject(fullObjectPath, null));

}

catch

{

throw;

}

}

 

where Interaction module contains procedures to interact with objects, applications and system. Runtime Helper class contains method for compiler construction. It is a service class and hence contains only static methods. Its GetObjectValue method boxes the object passed as the parameter. Boxing a value type creates an object and performs a shallow copy of the fields of the specified value type into the new object. Finally LateBinding is used as an object of type Object can be bound to object of any type i.e. it behaves as a varient type.
 
The first argument of LateBinding.LateCall method is the object on which the method should be called, second argument is the type of the object i.e. System.Type object, the third argument is the name of the method, 4th is set of arguments to be passed to that method, 5th argument is the set of names of the parameters to be passed to that method if they have any, and finally 6th argument is the set of booleans indicating whether to copy back any parameters.
Tell me isnt it very simple

How to handle New Row in the Grid Control

Recently I was developing some grid controls where I faced a situation where I want to programtically update the cell of a newly created row on its cell changed. The status of a New Row in the datagrid is always Detached until you changed the focus from that row to some other row, when its status changed to added. If you try to change the value of the cell in the row when its status is detached, it will not work and if you try to remove that row from its source and then add it, it will give error whenever you try to change its value that the row has already been added.
 
So I used the following code for the detached row and it worked:
 

//If it is a new row that has just been added but not attached to data grid, then ends its editing.

if(dr.RowState == DataRowState.Detached )

this.BindingContext[this.grid.DataSource, this.grid.DataMember].EndCurrentEdit();

A generic Equal Method

Recently I faced a situation where I want to write an override of Equal method so that I can compare any 2 of my business objects. So the best way to do is to override the Equals and GetHashCode method in the base class for all business objects. The following code is a very simple way to compare any 2 objects:
 

public override bool Equals(object obj)

{

PropertyInfo [] properties=this.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

foreach( PropertyInfo property in properties )

{

object obj1=property.GetValue(this, null), obj2=property.GetValue(obj, null);

if( (obj1==null && obj2!=null) || (obj1!=null && obj2==null) ||

((obj1!=

null && obj2!=null) && !obj1.Equals(obj2)))

return false;

}

return true;

}

public override int GetHashCode()

{

return base.GetHashCode ();

}

Copy constructor for child object

Many times we faced the situation where we want to do something like this:
 
A a=new B();
 
Where A is the child object and B is the parent which is not possible. We can achieve this goal by copy constructor like this:
 

public BaseEntity(ref object parent):this()

{

if(parent==null)

return;

FieldInfo [] fields;

if(parent.GetType()==this.GetType().BaseType)

fields=parent.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

else

fields=

this.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

foreach(FieldInfo field in fields)

{

object val=field.GetValue(parent);

field.SetValue(

this, val);

}

}