Skip to main content

Posts

ODP.Net issue for 10.2.0.4 driver

Please note that Windows 7 has a new Oracle Client (10.2.0.4). In most cases this should be simply the case of removing the existing reference to Oracle.DataAccess from your application and adding a new one from new client folder. However in one case, we came across rather an unusal problem. We call stored functions from a windows application which return Integer type value. Please note we are using RETURN type values not OUT type parameters as this a Function. Here is the C# code. /****************************/ OracleCommand cmd = new OracleCommand("DoesDataItemExist", oraCn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("RETURN_VALUE", OracleDbType.Int16, ParameterDirection.ReturnValue); cmd.Parameters.Add("p_ID", id); cmd.Parameters.Add("p_YEAR", year); oraCn.Open(); cmd.ExecuteNonQuery(); Int16 retVal = (Int16)cmd.Parameters["RETURN_VALUE"].Value; oraCn.Close(); return retVal > 0...

How to pass and Reterive data from Thread - Part 3

To reterive data from a Thread, you need a delegate callback methdo. step1 : Decalre the delegate for the callback method public delegate void CallbackMethod(int anynum); This delegare define the signature of the funtion which will be called by the Thread. Step 2: Amend you call to accept delegate method while creating instance of the class private CallbackMethod _callback; public TypeSafe(int num, string msg, CallbackMethod callback) { _num = num; _msg = msg; _callback = callback; } Step 3: Amend the ThreadProc (procedure that will be running on the Thread) to make a call to callback method public void ThreadProc() { Console.WriteLine(_msg, _num); if (_callback != null) { _callback(_num + 100); } } Step 4: Create a static function which you want to be called by the Thread. Note this should match the signature of the delegate public static void ResultCallback(int n) { Console.WriteLine("result from callback is {0}", n); } Step 5: P...

How to pass data to Thread - Part 2

Passing data to Thread using ParameterizedThreadStart  is the easiest way but not a type safe way as Thead.Start accepts any object. Alternative way is to encapsulate Thread procedure and data in a helper class and use the ThreadStart delegate to execute the Thread procedure. Make a note that you cannot return data from the Thread as there is no way to retrieve data from an asynchronous call. To retrieve data, you need callback method....which will look at part 3 of this article. Here is an example on how to pass data in a type safe way using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace ThreadTypeSafe { class Program { static void Main(string[] args) { TypeSafe ts = new TypeSafe(100, "This is message from a TypeSafe Thread with num as {0}"); Thread t1 = new Thread(ts.ThreadProc); t1.Start(); t1.Join(); ...

How to pass parameter to a Thread?

When a managed thread is created, the method that executes on the thread is represented by a ThreadStart or ParameterizedThreadStart delegate that is passed to Thread constructor For E.g. //to call a method with no parameters, use ThreadStart Thread t1 = new Thread(new ThreadStart(MyMethod)); //to call a method with parametets, use ParameterizedThreadStart Thread t1 = new Thread(new ParameterizedThreadStart(MyMethod)); To make it easy, Visual Basic and C# allow a short cut //short cut method Thread t1 = new Thread(MyMethod); Now the compiler works out which delegate constructor to use based on definition of MyMethod. To pass a parameter, similpy pass a value when you call Start on the Thread t1.Start(14); ///will pass 14 to MyMethod t1.Start("my message"); ///wiil pass "my message" to MyMethod //definition of MyMethod public static void MyMethod(object data) { //cast data to correct type and use data value //for e.g. output as string Co...

Immutable String and Performance

String of the type System.String is immutable in the .Net Framework. That means any change to a String causes the runtime to create a new string and abandon the old one. Guess how many string references following code will create? //c# String s; s= "one"; //"one" s += " two"; //"one two" s += " three"; //"one two three" s += " four"; //"one two three four" Console.WriteLine(s); If you guessed it four, you are right. Most developers think this is only one string. After running the code only the last string has a reference, the other three are disposed off during garbage collection. Avoiding these kind of temporary string helps avoiding unnecessary garbage collection and hence helps improve performance. There are several ways to avoid temporary string 1. Use the Concat, Join or Format method of the String Class to join multiple items into a single statment. 2. Use StringBuilder Cla...

Checking for Ajax Request

function AjaxRequest() { var request = null; if(typeof window.XMLHttpRequest != "undefined") {/*most modern browsers*/ request = new XMLHttpRequest(); } else if(typeof window.ActiveXObject != "undefined") {/*IE 6 browswer*/ try { request = new ActiveXObject("Microsoft.XMLHTTP"); } catch(err) { request = null; } } return request; }

Which Dictionary object should you use?

Hashtable SortedList String Dictionary List Dictionary Hybrid Dictionary NameValue Collection Retrieved by name Yes Yes Yes Yes Yes Yes Retrieved by Index Yes Yes Yes Yes Yes Yes Sorted by default No Yes No Key type Object String String (Strongly typed) Object Object String/ Integer Value type Object Object String Object Object Object Performance Optimised for fewer than 10 items Uses List Dictionary for small no of item. Changes to Hashtable as list grows Allows multiple value for a key

How to make a Custom Sort Function

You can create your won custome IComparer implementation to control sort order. While IComparable.CompareTo method controls the default sort order for a class, IComparer.Compare can be used to provide custom sort order. For e.g. following class which implements IComapere, provides custom reverse sort public class reverseSort : IComparer { int IComparer.Compare(Object y, Object x) { return ((new CaseInsensitiveComparer()).Compare(x, y)); } } //test class ArrayList al = new ArrayList(); al.AddRange(new String[] {"This", "is", "a", "Amazing", "world", "of", "Collections" }); //default sort order ascending al.Sort(); foreach (object o in al) { Console.WriteLine(o.ToString()); } //custom sort order al.Sort(new reverseSort()); foreach (object o in al) { Console.WriteLine(o.ToString()); }