發表文章

目前顯示的是 2月, 2019的文章

[VB6]bit-shift operation

圖片
VB6/VBA不支援 "<<" & ">>"的位元操作 (C/C++/C#) a = b << n ;b左移n bits a = b >> n ;b右移n bits (VB/VBA) a = b * (2 ^ n) '左移, 乘以2的n次方 a = b / (2^ n) '右移, 除以2的n次方 Demo code Option Explicit Sub bit_shift() Dim reg As Byte reg = &H1 ' 1 Debug.Print "Before: 0x" & Hex(reg) ' 2 reg = reg * (2 ^ 5) Debug.Print "left-shift 5-bits: 0x" & Hex(reg) ' 3 reg = reg / (2 ^ 2) Debug.Print "right-shift 2-bits: 0x" & Hex(reg) End Sub Result

[VB6]only user-defined types defined in public object modules can be coerced to or from a variant or passed to late-bound functions

圖片
自定義Type類型於module中, 無法傳入function/sub函數 因user-define type無法與variant相容 下面為error code Line 19, UC51 宣告為 DRIVER_INFO , 傳入 IsEmptyArray()的source為Variant-type 兩種type無法強制轉換 只能將 IsEmptyArray() 的 source 宣告為 REGISTER-type 但導致 IsEmptyArray() 只能接受 REGISTER-type , 無法接受其他data type Option Explicit Private Type REGISTER name As String addr As Byte bank As Integer End Type Type DRIVER_INFO strID As String reg() As REGISTER End Type Sub main() Dim UC51 As DRIVER_INFO Dim tmpBuf() As String Debug.Print IsEmptyArray(tmpBuf) Debug.Print IsEmptyArray(UC51.reg) ' error here End Sub Function IsEmptyArray(source As Variant) As Boolean On Error GoTo ERROR_HANDLER If UBound(source) >= 0 Then Exit Function End If ERROR_HANDLER: IsEmptyArray = True End Function

[VB6]強制轉換變數類型 Type conversion functions

圖片
Syntax CBool( expression ) CByte( expression ) CCur( expression ) CDate( expression ) CDbl( expression ) CDec( expression ) CInt( expression ) CLng( expression ) CLngLng( expression )  (Valid on 64-bit platforms only.) CLngPtr( expression ) CSng( expression ) CStr( expression ) CVar( expression ) Resource https://docs.microsoft.com/zh-tw/office/vba/language/concepts/getting-started/type-conversion-functions

Hard Code

- Wiki 寫死(英語:Hard Code或Hard Coding)是指在軟體實作上,將輸出或輸入的相關參數(例如:路徑、輸出的形式或格式)直接以常數的方式撰寫在原始碼中,而非在執行期間由外界指定的設定、資源、資料或格式做出適當回應。一般被認定是種反模式或不完美的實作,因為軟體受到輸入資料或輸出格式的改變就必須修改原始碼,對客戶而言,改變原始碼之外的小設定也許還比較容易。 https://zh.wikipedia.org/wiki/%E5%AF%AB%E6%AD%BB - 軟體開發必須面對的Dirty Code https://www.ithome.com.tw/node/83209 - 擁抱改變,遠離Hard Code http://teddy-chen-tw.blogspot.com/2018/09/hard-code.html