布尔是.NET中的两个字节?




这个问题源于 here.我在VS中尝试了这个声明,看看会发生什么:
Len(Name <= 3)

根据this answerthis one,布尔应该消耗4个字节.根据MSDN,Len Function in VB

Returns an integer containing either the number of characters in a
string or the nominal number of bytes required to store a variable.

名称< = 3应该将3转换为String并执行字符串比较,返回一个布尔值,所以Len应该评估它的字节数,应该是4.由于某些原因,上面的代码返回2,而不管第二个参数.换句话说,Len(True)也返回2.试图针对不同的平台目标(32和64) - 同样的事情. 为什么Len(Boolean)返回2而不是4?

With user-defined types and Object variables,the Len function returns the size as it will be written to the file by the FilePut function. If an Object contains a String,it will return the length of the string. If an Object contains any other type,it will return the size of the object as it will be written to the file by the FilePut function.

Len()是一个遗留函数,它只能用在从上一代Visual Basic项目中移植的代码中.它通常出现在涉及二进制串行化VB值的代码中.这些旧版本的基本类型尺寸不同.例如,整数为16位,解释了在使用Long的Web中发现的大量不良pinvoke声明.而布尔是一个VARIANT_BOOL的引擎盖,一个16位的值.更奇怪的是,将其True值转换为-1,而不是1.

明确采用.NET的大小对于由VB6程序编写的文件中存在的大量数据来说是一个很大的变化.或通过TCP连接发送的二进制数据.等等.因此,Len()函数返回传统大小.

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...