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#.