Wednesday, 8 October 2014

Java - Varargs

Varargs is a very good feature introduced in Java 1.5.  This feature allows programmers to pass variable number of argument of same type to a method.  Some may confuse it with overloading.  But it is actually different.  We will learn the differences between overloading and varargs below.

Difference between method overloading and varargs

Overloading.
1. Method signature may contain arguments of different data types.
Eg. public long add(int i,long l)



Varargs
1. varargs parameter in method signature will be of same data type.
    public long add(long l1, long ... numbers)

2. The varargs parameter should be last of the arguments to the method. If a method has only one parameter of varargs type, it is allowed.

Eg 1. Please see the above example.
Eg 2. Invalid code.
       public long add(long ... numbers, long l1)

Eg. 3 public long add(long ... numbers)


Code Sample:



public class VarargsDemo {
    
    /*
      The following method uses varargs parameter (numbers).
      Java considers varargs parameter as an array.
      So the variable numbers can be used as an array of integers.
    */

public int add(int n1)
{
System.out.println("Calling add method with 1 argument : ");
return n1;
}

public int add(int n1,int n2)
{
System.out.println("Calling add method with 2 arguments : ");
return n1 + n2;
}

public int add(int n1,int n2,int n3)
{
System.out.println("Calling add method with 3 arguments : ");
return n1 + n2 + n3;
}

    public int add(int n1,int ... numbers)
{
System.out.println("Calling add method varargs as argument : ");
int sum=n1;
for(int i=0;i<numbers.length;i++) {
sum = sum + numbers[i];
}
return sum;
}

public double average(double n1, double ... numbers)
{
double sum=n1;
for(int i=0;i<numbers.length;i++) {
sum = sum + numbers[i];
}
return sum/(numbers.length+1);
}

public static void main(String[] args)
{
VarargsDemo vd=new VarargsDemo();
// Sample usage of add Method
System.out.println("The sum of all numbers is : "+vd.add(1,2));
System.out.println("The sum of all numbers is : "+vd.add(1,2,3,4));
System.out.println("The sum of all numbers is : "+vd.add(1));
System.out.println("The sum of all numbers is : "+vd.add(10,11,12,1000,12,11,5000,12000));

// Sample usage of average method which uses varargs
System.out.println("The average of all numbers is : "+vd.average(1.25,2.75));
System.out.println("The average of all numbers is : "+vd.average(1.0,2.0,3.5,4.75));
System.out.println("The average of all numbers is : "+vd.average(1.0));
System.out.println("The average of all numbers is : "+vd.average(10.0,11.12,12.23,1000.34,12.45,11.56,5000.0,12000.0));
}
}


Output: 


Calling add method with 2 argument : 
The sum of all numbers is : 3
Calling add method varargs argument : 
The sum of all numbers is : 10
Calling add method with 1 argument : 
The sum of all numbers is : 1
Calling add method varargs argument : 
The sum of all numbers is : 18056
The average of all numbers is : 2.0
The average of all numbers is : 2.8125
The average of all numbers is : 1.0
The average of all numbers is : 2257.2125


Learning:

1. When we have two methods a. overloaded method and b. varargs method,  when we invoke a method with number of arguments which can fit into the overloaded method, then  the overloaded method will be called.  

Eg. In the above code, both the following methods can be used to invoke add() with 2 arguments.  If we invoke add(10,20), the method (a) will be called instead of method (b).

a. add(int n1,int n2) 
b. add(int n1,int ...numbers) 

Mysql - Alter/Remove default value of a column

Mysql Tech Byte: 
We can remove just the default values of a column(s) in Mysql using the following command. 

Single Column: 
ALTER TABLE < TableName > ALTER < ColumnName > DROP DEFAULT; 

If we execute the above statement, the default value for that particular column will be dropped. 

Multiple Columns: 
ALTER TABLE < TableName > ALTER < ColumnName1 > DROP DEFAULT, ALTER < ColumnName2 > DROP DEFAULT; 

Modify a default value: 

ALTER TABLE < TableName > ALTER < ColumnName1 > DROP DEFAULT, ALTER < ColumnName2 > SET DEFAULT 100; 

This statement removes a default value for ColumnName1 and alters the DEFAULT value of ColumnName2 from its previous value to the new value 100.

Wednesday, 3 October 2012

Language & Country in C#

1. To get the language in full Text like (English/Dutch) use the following code.

CultureInfo.CurrentCulture.Parent.DisplayName

2. To get language in two letter form like (en/ja . .) use the following code.

CultureInfo.CurrentCulture.Parent.Name;

3. To get Country in full Text like "United States/Japan" use the following Code

RegionInfo.CurrentRegion.DisplayName;

4. To get Country in two letter format like (US/JP) use the following code.

RegionInfo.CurrentRegion.Name;

Hope this helps someone.

Have a nice day.
Edwin.

Friday, 23 March 2012

MFC Dialog - Simulate a Mouse Click


Beginner Series.

Sometimes when you are trying to select a row in a listbox (CListBox) which is place in your by name <YourDialogName>, you might do all that should be done to select a row programatically.  But when nothing else works, here is a short cut.  You might notice that if you click on an entry in a list box, you selchanged method will be called.  But when you do a "SetCurSel", it might not be called, because your row will not get selected.  In that case the following short cut works.  You may vary the time interval given in Sleep method based on your mouse configuration.

void <YourDialogName>::SelectRow(int row)
{
     //COMMENT: listBox_URLis the name of the list box, whose row should be selected.


     RECT r; 

     listBox_URL.GetItemRect(row,&r);

     int x=r.left + 10, y= r.top+5;
     const int lParam=y<<16 | x;  // Infuse the X & Y co-ordinates into the lParam
     const int wParam=0;
     listBox_URL.SendMessage(WM_LBUTTONDOWN,wParam,lParam);
     Sleep(20);
     listBox_URL.SendMessage(WM_LBUTTONUP,wParam,lParam); 
}

MFC Dialog - Escape/Enter key disable

Beginner Series


When we start to program using an MFC Dialog, handling the Escape key and Enter key seems to be a challenge.  One way is to override the PreTranslateMessage of the Dialog.
Following is a code snippet


Please replace the <DialogName> with the actual name of the dialog which you have created.  


1. In the header file <DialogName>.h file add the following method, under the protected access modifier. 


virtual BOOL PreTranslateMessage(MSG* msg);


2. In the <DialogName>.cpp file add the following code



BOOL <DialogName>::PreTranslateMessage(MSG* msg)
{
       //COMMENT: Get the ID from which this message is received.
         // If the control matches, then see whether the Windows message is KEYDOWN. 
         // Then check whether the key pressed is Enter key.  Return false.  
         // By returning false, we are instructing the dialog to avoid the default behavior, which might 
         // be to close the dialog.


UINT ctrlID=GetDlgCtrlID(msg->hwnd);
if(ctrlID==<YOUR_CONTROL_ID> )  
{
if(msg->message == WM_KEYDOWN)
{
if(msg->wParam==VK_ENTER)
{
return FALSE;
}
}
}

if(msg->message == WM_KEYDOWN)
{
if(msg->wParam == VK_ESCAPE)
{
return FALSE;
}
}
      // For all the others, please go ahead with the default behavior.  

      return CDialog::PreTranslateMessage(msg);
}

Saturday, 3 March 2012

How to read from registry and display a value.

The Windows Registry is an integral part of the Windows Operating System.  Many of the configurations are not explicitly provided to users.  But folks from Microsoft decide to pin it down only in the registry.  So if you are into Windows programming eventually there will be a need for reading from and writing to registry.  The following examples do just that.  I am newbie to Windows Programming though have a substantial experience and interest in programming.

Code Sample:

The following code sample provides a simple functionality.  This part of code reads the registry key responsible for supressing the java-script error dialog when we browse web pages using Internet Explorer.

Registry Key : HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main

Key Type      : REG_SZ  // Null Terminated string value

void readEntriesFromRegistry()
{
     wchar_t disableScriptDebugger[128]=L"Software\\Microsoft\\Internet Explorer\\Main";
     wchar_t keyValue[3]; // I know the value might be either 'yes' or 'no'
     int keySize=sizeof(wchar_t)*3; // Size in bytes.
     int          errorCode=-1;  // Initialize errorCode
     HKEY hKey=0;             // Initialize key
    // Open Registry key.
 errorCode=RegOpenKeyEx(HKEY_CURRENT_USER,disableScriptDebugger,0,KEY_READ,&hKey);

     if(errorCode==0)
     {
          // Read the value for the registry key.  The value is stored in keyValue
   errorCode=RegQueryValueEx(hKey,L"Disable Script Debugger",0,&keyType,(LPBYTE)keyValue,&keySize);
     }
     CString str(keyValue);
     ATLTRACE(L"The value is : %s \n",str);
    RegCloseKey(hKey);
}  

Note:
If you use the above code snippet repeatedly allocate memory for keyValue and size for keySize everytime, since you may get errorCode as 234 instead of 0.















}

Saturday, 25 February 2012

Oracle SQL Fundamental 1 - Practice Exercise 2


1. select last_name, salary from employees where salary > 12000
2. select last_name, department_id from employees where employee_id=176
3. select last_name, salary from employees where salary not between 5000 and 12000
4. select last_name,job_id,hire_date from employees where last_name in ('Matos', 'Taylor')
5. select last_name, department_id from employees where department_id in (20,50)
6. select last_name, salary from employees where (salary between 5000 and 12000) AND DEPARTMENT_ID in (20,50)
7. select last_name, hire_date from employees where hire_date >='01-JAN-1994' and hire_date<='31-Dec-1994'
8. select last_name, hire_date from employees where manager_id is null
9. select last_name, salary, COMMISSION_PCT from employees where commission_pct is not null
10. select last_name, salary from employees where salary>&salary_amount
11. select employee_id,last_name,salary,department_id from employees where manager_id =&ID_OF_MANAGER order by &ORDER_BY_COLUMN
12. select last_name from employees where last_name like '__a%'
13. select last_name from employees where last_name like '%a%e%' or last_name like '%e%a%'
14. select last_name,job_id, salary from employees where job_id in ('SA_REP','ST_CLERK')  and 17. salary not in (2500,3500,7000)
15. select last_name, salary, commission_pct, salary * commission_pct "Commission" from employees where commission_pct=.2