Monday, July 7, 2008

Passing Reference in C#

In the article Passing Reference-Type Parameters, we've seen that we need to use the "ref" keyword in order to swap string values. This is also true for any other reference-types such as an ArrayList.

Remember:
When you pass a reference-type parameter by value, it is possible to change the data pointed to by the reference, such as the value of a class member. However, you cannot change the value of the reference itself; ...

This reminds me the differences between a reference and a pointer: In the swap function, if we are passing parameters by pointer(like in C or C++), then any local change is also global; if we are passing parameters by reference (like in C#), then any local change is not global.

Monday, March 31, 2008

Every keyCode is 229?!

I need to blog this. When I was debugging javascript, I found that no matter I press "A" or "X", the keyCode of it is 229. It turned out that I was typing using Microsoft PinYin. Although the mode is English instead of Chinese, it is Chinese input keyboard anyway. That's why the keyCode behaves strangly.

Sunday, March 2, 2008

Constant Pointers and Pointers to Constants

1. Pointer to Constant
char char_A = 'A';
const char * myPtr = &char_A;
*myPtr = 'J'; // error - can't change value of *myPtr

2. Constant Pointer
char char_A = 'A';
char char_B = 'B';

char * const myPtr = &char_A;
myPtr = &char_B; // error - can't change address of myPtr

Difference between Reference and Pointer

Difference between Reference and Pointer in C++

  1. Reference should be initialized and pointer need not be.

  2. Reference can’t point to NULL and pointer can.

  3. Reference can’t be changed to some other objects it is once initialized but pointer can be.

  4. Pointer can be incremented or decremented and subjected to any pointer arithmetic operations, but reference can’t.

Sunday, February 10, 2008

Quotes

Half the world is composed of people who have something to say and can't; the other half have nothing to say and keep saying it.

If anything can go wrong, it will.

Chance favors the prepared mind.

Wednesday, January 30, 2008

Add() method in .NET (passing parameter)

Collection objects have Add() method in .NET. It is very important to understand how it works.

Code Block 1:
TableRow row = new TableRow();
for (int i = 0; i < resultFields.Count; i++)
{

foreach(string str in resultFields[i])
{
TableCell cell = new TableCell();
cell.Text = str;
this.Response.Write(str);
row.Cells.Add(cell);
}
tbl_rsltFields.Rows.Add(row);
}
In this example, if resultFields.Count is 2, how many rows do you think are in the tbl_rsltFields?
The answer is 1. It is worth noticing this case.

Code Block 2:
for (int i = 0; i < resultFields.Count; i++)
{
TableRow row = new TableRow();

foreach(string str in resultFields[i])
{
TableCell cell = new TableCell();
cell.Text = str;
this.Response.Write(str);
row.Cells.Add(cell);
}
tbl_rsltFields.Rows.Add(row);
}
In this case, the answer is 2.

Actually, if we try to add "row.Cells.Clear();" right after "tbl_rsltFields.Rows.Add(row)" in code block 1, there will be nothing contained in tbl_rsltFields at last. Based on this fact, we know that by calling Add() method, it only gets a copy of the reference, instead of making a copy of the whole row. So it acts like it is passing reference by value.

Wednesday, January 23, 2008

Application["UserCount"] += 1 doesn't work

Why does Application["UserCount"] += 1; incur compiler error? It's the same reason why Application["UserCount"] = Application["UserCount"] + 1; doesn't work either.

And once you know the correct way should be: Application["UserCount"] = (int)Application["UserCount"] + 1; you'll also know (int)Application["UserCount"] += 1; is wrong too.