Monthly Archives: March 2012



當程式裡需要將民國年的字串轉換成DateTime格式的時候,我使用了土法煉鋼的方式,Date.Parse().AddYear(1911),先轉換再加1911年。還好沒幾天馬上碰到101/2/29!當Date.Parse(String)的時候,其實是將字串轉換為西元年,西元101年當然沒有2/29號…因此會出現”字串未被辨認為有效的 DateTime”Exception。而且這麼做在民國99年(含)以前出來的日期也全部錯誤。先將字串的年份加上1911也許是可行的方法,但是會有多餘的型態轉換,而且不夠彈性。

.NET本身就有對應世界各國日曆格式的方式,可以使用Date.Parse(String, IFormatProvider)Date.ToString(IFormatProvider),IFormatProvider參數可以是CultureInfoDateTimeFormatInfo,在其中指定DateTimeFormatInfo.Calendar屬性為TaiwanCalendar,就可以正確的轉換與顯示民國年。



Read the rest of this entry »

Leave a comment

Posted by on March 23, 2012 in .NET, VB



VB Left() and Right() equivalent in VB.NET

When trying to rewrite VB string manipulation codes into .NET framework version, old VB Left() and Right() methods could be seen in the lines very often. At first I rewrote them as :

'Public Function Substring ( _
'    startIndex As Integer, _
'    length As Integer _
') As String

' str = Left(str, length) equivalent
str = str.Substring(0, length)
' str = Right(str, length) equivalent
str = str.Substring(str.Length - length)

Generally this works fine but could potentially cause startIndex and length out of range exception in conditions like:

Dim str As String = "01234"
str = Left(str, 6)           'str = "01234"
str = str.Substring(0, 6)    'Exception

There’s a much more feasible solution on stackoverflow by Noldorin which uses Math.Min() for Left() to make sure the out of range exception won’t happen. Here are the refined equivalents according to Noldorin’s solution:

' str = Left(str, length) equivalent
str = str.Substring(0, Math.Min(length, str.Length))
' str = Right(str, length) equivalent
str = str.Substring(Math.Max(str.Length, length) - length)
Leave a comment

Posted by on March 20, 2012 in .NET, VB



Convert number to words in English

The methods below are rewritten from How to convert a numeric value into English words in Excel to VB.NET equivalents. It takes string type arguments of numbers like “12,345.6789”, rounds it down to the second decimal place which becomes “12,345.67” and spells it as “Twelve Thousand Three Hundred Forty Five Dollars and Sixty Seven Cents”.

Read the rest of this entry »

Leave a comment

Posted by on March 20, 2012 in .NET, VB



Why is DataGridView slow

I have an about 100 rows DataGridView which drops performance significantly since I wrote some code modifying its cells value programmatically. After looking up the MSDN post, the property AutoSizeColumnsMode is the reason why my DataGridView being slow. The property was set to AllCells, turns out it’ll auto resize cells when the value changes and that takes a lot of resource.

By 100 rows my code will run for about 1500 milliseconds. Changing the property to DisplayedCells can reduce it to 30 milliseconds and changing to ColumnHeader can reduce it to less than 8 milliseconds. It’ll cost no time if I commented the value changing code.

I guess every single cell value change will always auto resize according to the value of the AutoSize property, even when the property value is ColumnHeader.

Leave a comment

Posted by on March 13, 2012 in .NET, Winform


Tags: , ,

How to get the integer part of a given number

In Visual Basic we could use Fix() and Int() to get the integer part of a given numeric value. But they handle negative values differently, I’ll simply show some examples as follows:

number Fix(number) Int(number)
12.2 12 12
11.8 11 11
-9.3 -9 -10
-9.8 -9 -10
-10.2 -10 -11

Here is the code to compare both function:

Private Sub TestGetIntegerPart()
    Dim numbers() As Double = {12.2, 11.8, -9.3, -9.8, -10.2}

    Dim dt As New DataTable
    Dim row As DataRow

    For Each num As Double In numbers
        row = dt.NewRow()
        row("number") = num
        row("Fix") = Fix(num)
        row("Int") = Int(num)


    Me.DataGridView1.DataSource = dt
End Sub
Leave a comment

Posted by on March 8, 2012 in .NET, VB





Read the rest of this entry »

Leave a comment

Posted by on March 8, 2012 in .NET, VB



Prompt a MessageBox for confirmation

I need a MessageBox with OK and Cancel buttons for users to confirm. Here is an easy sample:

Dim result As DialogResult
result = MessageBox.Show("Are you sure this is OK?", "MyTitle", MessageBoxButtons.OKCancel, Nothing, MessageBoxDefaultButton.Button1)

If result = DialogResult.OK Then
    ' User presses OK button.
    ' Do something.
End If
Leave a comment

Posted by on March 6, 2012 in .NET, VB