Tuesday, December 25, 2012

cordova 2.2.0 messages.slice error in eclipse (android)

When using cordova 2.2.0 in eclipse for android development if you see following error

TypeError: 'undefined' is not a function (evaluating 'messages.slice(0, spaceIdx)') at file:///android_asset/www/cordova-2.2.0.js:1113

No need to worry, just do the following

Change line 1101

messages = messagesFromNative.shift();
to
messages = messagesFromNative.shift() + '';


Wednesday, September 28, 2011

Dynamically Calculate Control Dimension at UI

Calculate the dimension of controls at UI in term of rows*columns. This accepts parent container width/height and number of controls to draw. You may also provide what ratio you need for controls.


/// Calculate control dimension as per parent size and required ratio
/// param name="ParentWidth">Parent control width
/// param name="ParentHeight">Parent control height
/// param name="TotalControls">Total control
/// param name="RatioX">X ratio of control to maintain
/// param name="RatioY">Y ratio of control to maintain
/// param name="TotalColumns">get columns
/// param name="TotalRows">get rows
public void CalculateDimensions(int ParentWidth, int ParentHeight, int TotalControls, int RatioX, int RatioY, out int TotalColumns, out int TotalRows)
{
TotalColumns = TotalRows = 1;

      double areaRatio = Math.Sqrt(((double)(ParentHeight * RatioX) / (double)(ParentWidth * RatioY)) / (double)TotalControls);
      double expectedWidth = areaRatio * ParentWidth;
      double expectedHeight = areaRatio * ParentHeight;

      if (ParentWidth > ParentHeight)
            TotalColumns = (int)Math.Ceiling((double)ParentWidth / (double)expectedWidth);
      else
            TotalRows = (int)Math.Ceiling((double)ParentHeight / (double)expectedHeight);

bool isOK = false;
      while (!isOK)
      {
            if (ParentWidth > ParentHeight)
            {
                  TotalRows = (int)(Math.Ceiling((double)TotalControls / (double)TotalColumns));
                  if (TotalColumns >= TotalRows) isOK = true; else TotalColumns++;
            }
            else
            {
                  TotalColumns = (int)(Math.Ceiling((double)TotalControls / (double)TotalRows));
                  if (TotalRows >= TotalColumns) isOK = true; else TotalRows++;
            }
       }
}

Thursday, June 30, 2011

C# Shortcuts (Automatic Properties)


C # 3.0 introduces automatic properties. A property is usually (but not have to) to a private variable that is exposed to the outside world through getters and setters. The following is a common example of this



public class Employee
{
 private string _fName;
 public string FName
 {
    get { return _fName; }
    set { _fName = value; }
 }
}
Now see the magic...
public class Employee
{
 public string FName { get; set; }
}
C # compiler automatically creates a variable background and the right to get and set properties. Why is it useful? After all, you could have just done a string variable instead of a public class.

When you define as a property allows you to add validation logic in the current class at a later stage. The signature in the memory of the class will not change which means that any external library compiled code need not be recompiled

C# Shortcuts (Nullable objects)

The variable must have a value, can not be empty. Sometimes it would be convenient, it was possible to give a "null" (for example, undefined) variable. . NET 2.0 Nullable general use, that makes this possible. The next two lines to produce exactly the same purpose:


Nullable myVar = null;
or use this...
int? myVar = null;
By one ? According to a definition of the variable, the compiler will wrap a Nullable  generic type.

C# Shortcuts (Alias ​​long namespaces and types)


The names of the identifiers of C # can be very long. For example if you are automating Microsoft Office in C #, you may want to do something as simple as MS Word to open and edit a document. You can use the "use" to create an alias for a class or namespace.


using ShortWord = Microsoft.Office.Interop.Word;
Now in code simply use ShortWord where ever you want...
ShortWord.Application = new ShortWord.Application() { Visible = True; }


C# Shortcuts (Using Statement)


Often, you will need to allocate a system resource or network resources, etc. Each time you need such a resource, there are three crucial steps to go through:

You will have the resource you use, and then get rid of it. If you forget to properly dispose of it, you create a memory or resource leaks. This is best illustrated through the following models


// Step-A. Allocation of desired object here
Font myFont = new Font("Arial", 12.0f);
try
{
     // Step-B. use the resource (myFont) here
}
finally
{
     // Step-C. Dispose your object here
     if (myFont != null)
        ((IDisposable)myFont).Dispose();
}
Using allows us to use to compress this to:
//Allocate the desired resource here
using (Font myFont = new Font("Arial", 12.0f))
{
    // Use the resource here
}
// The best part is that Disposal is automatic.


C# Shortcuts (Object Initializers)


When you create a new object, it is often necessary to assign one or more of its properties. The introduction of C # 3.0 you can now use object initializers, and to improve the readability of this, and reduce your code


Employee emp = new Employee();
emp.Name = "Mr Smith";
emp.Designation = "Driver";

Now make it short like this...
Employee emp = new Employee {emp.Name = "Mr Smith", emp.Designation = "Driver"};