Friday, July 20, 2007

What I've learn from COOP

1. Enterprise-scaled project. More stuff to concern: multi-threading, better use on resources(memory etc.), structure flexibility, code reusability, refactoring.

2. Testing. Software engineering knowledge and experience.

3. Documenting code and project. Also improved writting skills.

4. Interpersonal skills. Working with peers and supervisors. Presentation. Book club.

Thursday, July 12, 2007

Avoid Pitfalls

The following code snippet is error-prone:

for (int i = 0; i <>SelectedIndices->Count; i++)
{
/* do something */
}

Why? listView->SelectedIndices->Count may change in the loop. For example, if we delete one item in listView->SelectedIndices collection a time, listView->SelectedIndices->Count will be 1 less than the the value in last loop.

Another common pitfall is, when deleting items in a collection, items after the deleted item are shifted automatically. Therefore, we are to delete some items one by one in a collection according to their initial indices, we need to delete them backwards.

Friday, July 6, 2007

Making Textbox control autoscrollable

If text is appended every time, we can do this way:

txtBx->AppendText(strToAppend);
txtBx->Focus();
txtBx->ScrollToCaret();

If text is completely loaded from the beginning to the end every time, it can be done this way:

txtBx->Text = strContent;
txtBx->Focus();
txtBx->SelectionLength = 0;
txtBx->SelectionStart = txtBx->Text->Length;
txtBx->ScrollToCaret();

In either case, Focus method is indispensable.

Thursday, July 5, 2007

Implementing indexers in VC++

Unlike C#, C++ doesn't support indexers directly. However, we can achieve the same goal through a different way:

[System::Reflection::DefaultMemberAttribute(S"Item")]
public __gc class PlayoutLists : public ICollection
{
public:
ArrayList* listArray;
public:
PlayoutLists()
{
listArray = new ArrayList();
}
public:
__property PlayoutList* get_Item(int index)
{
return dynamic_cast(listArray->get_Item(index));
}
public: void CopyTo(Array* a, int index)
{
listArray->CopyTo(a, index);
}
public: __property int get_Count()
{
return listArray->get_Count();
}
public: __property Object* get_SyncRoot()
{
return listArray->get_SyncRoot();
}
public: __property bool get_IsSynchronized()
{
return false;
}
public: IEnumerator* GetEnumerator()
{
return listArray->GetEnumerator();
}
public: void Add(PlayoutList* newList)
{
listArray->Add(newList);
}
};

get_Item (and set_Item if you want to be able to set the value) property will work the same way as indexers in C#.

Thursday, June 28, 2007

Beware of "==" in VC++

"==" operator of String class may not return desired result. For example, when checking the equality of two Strings:

if (cmbBx_Pos->Text == S"BOL")

Even if the two Strings are the same, it may have a "false" result. To make sure it return the correct result, use this instead:

if (cmbBx_Pos->Text->Equals(S"BOL"))

It doesn't make much sense why the "==" operator failed. Perhaps it was the operator of Object class, so it was comparing two references instead of two Strings.

Updated: when using a .net string literal in VC, always write as: S"hello". "==" operator will return false when comparing "hello" and S"hello". If a project has only .net strings as S"xxx", using "==" operator probably has no problem. If a project deals with different kinds of string, we better off use String::Equals to check string equality.

Thursday, June 21, 2007

Rules That Make Your Programming Life Easier

Rule 1: Life is like an onion: you peel it one layer at a time, and sometimes you weep. So is programming.

Rule 2: Internet is one of your most reliable friends.

Rule 3: Whenever encounter a problem, search on Internet, refer to books and think. Until you know exactly what the problem is, don't ask for help.

Rule 4: Don't bother (ask tech questions etc.) colleagues too often. People are all busy!

Rule 5: Taking notes is very very important. If you have a doubt, read Rule 1 five times.

Tuesday, June 12, 2007

Access form controls freely

Sometimes we want to access form controls outside the normal scope. For example, a callback function in another file need to notify some control so it can update its display. Simply change the control from private to public is not enough, we also need to get the current form instance. According to the article How to get current ApplicationContext instance at run time, we can create a new class MyForm which inherits Form class and set a similar static field _myForm. The static field will always store the first instance of that class, even if that class is initialized multiple times later.

We then use this new class to run the application:

Application::Run(new MyForm());

Later on, whenever we want to get the instance of MyForm, we can do this:

MyForm* myForm = (new MyForm())->_myForm;

Although this method may not deserve the word "pattern", it is interesting to compare it to the Singleton Pattern. Different from Singleton Pattern, we can have more than one instance in this case, but we can freely get the first instance.