Tuesday, August 21, 2007

Tuesday, August 14, 2007

Get used to managed C++ style definition

Sometimes the ugly managed c++ grammar can cause us much pain.

For example, what is the correct way of defining a __wchar_t gc array?

Wrong answers:
__wchar_t charArr[] = {':', '\\'};
__wchar_t[] charArr = {':', '\\'};
__wchar_t __gc[] charArr = {':', '\\'};
__wchar_t __gc charArr[] = {':', '\\'};
__wchar_t __gc[] charArr[] = {':', '\\'};
JUST_LET_ME_DEFINE_A_DAMN __wchar_t __gc charArr[] = {':', '\\'};

Only this one is right:
__wchar_t charArr __gc[] = {':', '\\'};

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.