October 8, 2008

[.NET] - Bind two controls to the same data source with synchronization

Photobucket

This is very common problem, as a newcomer I had spent great deal of time to solve this. Suppose you have two controls viz, Combobox and Listbox on same form and Datasource property of both these controls is same. Then the selection in one control selects same item in other control. This problem occurs because of BindingContext property of controls. By default the BindingContext member of each of the two controls is set to the Form's BindingContext. Thus, the default behavior is for the ComboBox and Listbox to share the same BindingContext, and that's why the selection in the ComboBox is synchronized with the current item of Listbox. If you do not want this behavior, create a new BindingContext member for at least one of the controls.

comboBox1.DataSource = dataset.Tables[ "Items" ]; 
comboBox1.ValueMember = "CustomerID"; 
comboBox1.DisplayMember = "CustomerID"; 

// Set the BindingContext property of ListBox to new BindingContext
listBox1.BindingContext = new BindingContext(); 
listBox1.DataSource = dataset.Tables[ "Items" ]; 
listBox1.ValueMember = "CustomerID"; 
listBox1.DisplayMember = "CustomerID";

[.NET] - Using Anchoring and Docking

Although both anchoring and docking are useful while resizing windows application, the anchor can keep control of its size and its location in relation to control container when the container is not attached to particulae edge of the container, but somewhere in the middle. 

Photobucket
The Docking feature attaches a control to the edge of its container. When a control is docked to an edge of it's container, it will always stick to that edge. The control will also resize itself to fully fit the edge of the container and also resizes as the container is resized. The Dock property of the Form control can be set to any one of the six values, Top, Bottom, Left, Right, Fill, and None.

Photobucket
Anchoring specifies how a control anchors to the edges of its container. When a control is anchored to an edge of its container, the distance between the control and the specified edge remains constant when the container resizes. For example, if a control is anchored to the right edge of its container, the distance between the right edge of the control and the right edge of the container remains constant when the container resizes. A control can be anchored to any combination control edges. If the control is anchored to opposite edges of its container (for example, to the top and bottom), it resizes when the container resizes.

October 7, 2008

[.NET] - Readonly Constants


There are situations where we would decide the value of a constant member at the time of execution. May we also have different constants of different objects of the class. To overcome these shortcomings, C # is amending another known as reading to be used with data members. This amendment seeks to fix the value of using a constructor method, but can not be changed later. Members readonly May be declared as static fields or fields example. When they declared fields example, they may take different values with different objects. Examine the code below:

class Numbers
  {
    public readonly int m;
    public static readonly int n;
    public Numbers (int x)
    {
      m=x;
    }

    static Numbers ()
    {
      n=100;
    }
  }

The value of m is provided at the creation of an object using the constructor with the parameter x. This value remains constant for this object. Remember that the variable is assigned a value of 100, even before the creation of all instances.

[.NET] - Copy Constructors

We know that the manufacturer is a method that has the same name as the class and returns no value. 
It is used to initialize the data subject, we're creating.

There is another type of copy-builder the manufacturer. When you copy one object to another, C # allows you to copy the reference to the first objection to the new object, which means that we now have two references to the same object. To make a real copy, we can use a copy constructor, which is just a standard constructor that takes an object of class as its only parameter. For example, that a copy constructor of the class of students might look. Note that we copy the domain name for the new object.

public Student(Student student)
{
  this.name = student.name;
}

Now we can use this constructor to create a copy. The copy is another object, not just a reference to the original object.

class Student
{
  private string name;
  public Student(string name)
  {
    this.name = name;
  }
  public Student(Student student)
  {
    this.name = student.name;
  }
  public string Name 
  {
    get 
    {
      return name; 
    }
    set 
    {
      name = value; 
    }
  }
}

class Final
{
  static void Main()
  {
    Student student = new Student ("A");
    Student NewStudent = new Student (student);
    student.Name = "B";
    System.Console.WriteLine("The new student's name is {0}", NewStudent.Name);
  }
}

The new student's name is A.

September 19, 2008

[C/C++] - C++ optimizations (general)

1- Use 'int'

Always use the int data type instead of char or short wherever possible. int is always the native type for the machine.

2- Make Local Functions Static

Always declare local functions as static, e.g.,

static void foo()

This means they will not be visible to functions outside the .cpp file, and some C++ compilers can take advantage of this in their optimizations.

3- Avoid Expensive Operations

Addition is faster than multiplication and multiplication is faster than division. Factor out expensive operations whereever possible.

4- Pass By Reference

Always try to pass classes by reference rather than by value. For example, use

void foo(TMyClass &myClass)

rather than

void foo(TMyClass myClass)

5- Use 'op='

Wherever possible, use 'op=' in favour of 'op'. For example, use

myClass += value;

rather than

myClass = myClass + value;

The first version is better than the second because it avoids creating a temporary object.

6- Inline Small Functions

Small, performance critical functions should be inlined using the inline keyword, e.g.,

inline void foo()

This causes the compiler to duplicate the body of the function in the place it was called from. Inlining large functions can cause cache misses resulting in slower execution times.

7- Use Nameless Classes

Whereever possible, use nameless classes. For example,

foo(TMyClass("abc"));

is faster than

TMyClass myClass("abc");

foo(myClass);

because, in the first case, the parameter and the class share memory.