在 Office VBA 的 Microsoft 檔案中,該函式的頁面VarType()列出了它的回傳值。并且有一個包含“VarType 常量”串列的頁面,沒有任何解釋它們是什么或使用它們的位置,但該串列與回傳值相同VarType():
- https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/vartype-function
- https://docs.microsoft.com/en-us/office/vba/language/concepts/getting-started/vartype-constants
在 .Net VB 的 Microsoft 檔案中,有一個頁面列出了“VariantType Enum”的值,并說這個列舉“指示 VarType 函式回傳的變體物件的型別。”:
- https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.varianttype
.Net VB 函式上有一個頁面:
- https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/functions/
帶有指向功能類別的鏈接,但這些類別似乎都不包含 function VarType()。所以我的第一個問題:.Net是否有特定的功能?VarType()如果是這樣,它記錄在哪里?
這些 Office 和 .NetVarType()值串列幾乎但不完全相同:
| VariantType 列舉 (.Net) | VarType 常量 (Office) | |
|---|---|---|
| 專案數 | 20 | 19 |
| 2 | 短的 | vb整數 |
| 3 | 整數( = vbInteger) |
vb長 |
| 18 | 字符 ( = vbChar) |
缺席的 |
| 20 | 長( = [vbLong) |
vb隆隆 |
顯然,VarType()Office 和 .Net 中的功能略有不同。并且為回傳值定義的一些常量在兩個平臺中具有相同的名稱但不同的含義。這似乎很草率。
我在 Office 程式、Excel 和 Access 中作業。我從來沒有在.Net 中作業過,我想如果我曾經將VBA 代碼從Office 移植到.Net,我將需要小心這些VarType()值的差異。
在 VariantType Enum 值串列中,大多數值都被宣告為等效于 Visual Basic 常量vb...。在上表中,我在括號中顯示了這些等價物。這些常量似乎是常量類的欄位:
- https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.constants
該類具有欄位vbInteger和vbLong,并且我在表中提供了指向這些欄位的檔案頁面的鏈接。但是該類似乎沒有欄位,vbChar.
所以我的第二個問題:常數vbChar存在嗎?如果是這樣,它是什么?是否記錄在案?
uj5u.com熱心網友回復:
Okay, there's been a lot of criticism of my question in the comments, and I think I can piece together an answer from what I learned there. Please tell me if this answer is wrong, possibly by posting a better one.
First I will mention that one of the comments said, "You simply cannot compare [VBA and VB.net], they're apples and oranges." That is flawed logic. It's perfectly valid to compare apples and oranges. In fact, it's only if apples were the same as oranges that there would be little point in comparing them. Comparison is about recognizing differences and similarities, and that's what I was doing in my question. I can compare Latin and Greek, Fortran and C, or VBA and VB.net. Upon comparing some aspects of the latter pair, my two questions were both about VB.net (or what I called ".Net VB"). If there was something wrong with my comparison, please correct me instead of telling me not to compare them.
- (a) Is there a function
VarType()specific to .Net? If so, where is it documented?
What I infer from the comments is that the function exists in VB.net for compatibility with other versions of VB, but is deprecated. Strangely, even though its purpose is compatibility with other version of VB and its output values are mostly the same as in VBA, some of them are different, which makes that compatibility complicated.
If there is documentation of it, nobody said where to find it.
- (b) Does the constant
vbCharexist? If so, what is it? Is it documented?
It exists in VB.net. It is a value of the enum VariantType. vbInteger and vbLong are also values of that enum.
vbInteger并且vbLong也是類的欄位,Constants因為它們與值VT_12和列舉VT_14相關聯(盡管該列舉不是.Net的一部分)。不是類的欄位,因為它與 的值無關。VarEnumvbCharConstantsVarEnum
vbInteger并vbLong擁有自己的檔案頁面作為類的欄位Constants。vbChar沒有自己的檔案頁面。它唯一的檔案是它出現在 enum 的值串列中VariantType。
uj5u.com熱心網友回復:
(a) .Net 中的 VarType() 是否與 Office 中的不同?
不,它們是相同的。它們在自動化庫中指定。它讀取變體標頭中的位元組。請參閱https://docs.microsoft.com/en-us/windows/win32/api/oaidl/ns-oaidl-variant。
(b) 什么是 vbChar?
構成 C 字串的 AC 概念,它是一個 vbchar 陣列(在 C 中稱為 char)。VB6/VBA6/VBA7 不使用 C 字串。雖然 C 字串包含在 B 字串中。當您傳遞一個字串ByRef而不是ByVal傳遞包含的 C 字串的地址而不是 B 字串的地址時。
這是什么意思。VB 程式員將用于Instr()訪問第 n 個元素。這是一個緩慢的函式呼叫。它需要設定堆疊。AC程式員也會Array(n)去做同樣的事情。這更快,因為它是讀取的baseofarray (nthelement * 32 bit)。
常量 vbChar 是否存在?如果是這樣,它是什么?是否記錄在案?
它以數字形式存在。它是 C 字串的一個元素。它記錄在自動化庫中。
請參閱https://docs.microsoft.com/en-us/windows/win32/api/wtypes/ne-wtypes-varenum
如果您VarType()在 VBChar 上運行,則會回傳該鏈接處的數字。
也許您應該在提問之前閱讀 VBA(表單包/編譯器 VB6 的語言)。你可以在這里得到這個https://docs.microsoft.com/en-us/openspecs/microsoft_general_purpose_programming_languages/ms-vbal/d5418146-0bd2-45eb-9c7a-fd9502722c74
編輯
這是您char的 VB6/VBA。
Declare Function VarI1FromI4 Lib "Oleaut32" (ByVal Code As Byte, ByRef Char As Variant) As Long
Sub Main()
Dim Code As Byte
Dim Char
Dim HResult As Long
Code = 65
HResult = VarI1FromI4(Code, Char)
MsgBox HResult
MsgBox VarType(Char)
End Sub
請注意,當您單步執行此代碼時,工具提示char會告訴您VB 不支持的自動化型別。
當您嘗試在其中使用它時,MsgBox它會由自動化庫自動轉換為函式定義所說的任何內容。
VC 6 和 VB6 是自動化庫的包裝器。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/421584.html
標籤:
上一篇:資料表上的總和列有2個不同的結果
下一篇:執行通過組態檔命名的方法
