VB6 类型别名 long
有什么方法可以在VB6中创建long类型别名吗?我知道你可以将用户类型定义为结构,但我需要类似于
typedef int mytypename;
C 中的东西,我只是简单地为一个简单类型添加别名
Is there any way to make a type alias for long in VB6? I know you can define user types as structures, but I need something similar to
typedef int mytypename;
in C where I am simply aliasing a simple type
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只能在可由 VB6 使用的自定义类型库中实现此类类型定义。
例如,
OLE_COLOR
、OLE_HANDLE
是在stdole.tlb
中声明的公共类型定义,可以在 VB6 中使用,如Dim clr As OLE_COLOR< /code> 相当于
Dim clr As Long
。You can only implement such typedefs in a custom typelib, that can be consumed by VB6.
For instance
OLE_COLOR
,OLE_HANDLE
are public typedefs declared instdole.tlb
and ready to use in VB6 as inDim clr As OLE_COLOR
being equivalent toDim clr As Long
.C 语言中这样做的唯一真正目的是支持可移植性。 C 标准并没有准确地告诉您各种数字类型在所有受支持的体系结构上的大小,它只是告诉您最小大小。因此,由于您可能需要更改底层实现类型,因此您可以使用
typedef
和代码的友好名称。VB 6 中的情况并非如此。所有内置类型的大小均已明确定义并保证不会更改。它是 VB 6 规范的一部分。所以实际上没有太多需要
typedef
或等效的东西。我想有些人在 C 中使用
typedef
是为了提高可读性,但我不认同这种用法。如果它是整型,请使用适当大小的整型。没有真正的可读性优势:唯一有意义的情况是您正在定义一个新的、成熟的类型,例如颜色。但在这种情况下,C 程序员通常使用
typedef
根据整型类型定义颜色类型,以节省空间和内存。同样,这在 VB 6 中是无关紧要的,因为如果您关心简约的内存使用和最大速度,那么您首先就不会在 VB 6 中编写代码。创建表示颜色类型的结构(用户定义类型)甚至类,然后在代码中使用它有更多优点。这样,您就可以获得所有可读性优势和类型安全性(这是在 C 中使用 typedef 无法获得的东西) 。如果您想在底层将
Color
类型实现为Long
,那是您的事。The only real purpose of this in C is to support portability. The C standard doesn't tell you exactly what the size of the various numeric types will be on all supported architecture, it just tells you the minimum sizes. Thus, since you might need to change the underlying implementation type, you use a
typedef
and a friendly name for the code.This isn't the case in VB 6. The size of all the built-in types is well-defined and guaranteed not to change. It's part of the VB 6 specification. So there's really not much need for a
typedef
or equivalent.I suppose some people use
typedef
s in C for readability, but I don't buy that usage. If it's an integral type, use the appropriately-sized integral type. There's no real readability benefit to:The only case where it makes sense is if you're defining a new, full-fledged type, like a color. But in that case, C programmers generally use a
typedef
to define the color type in terms of an integral type in order to save space and memory.Again, that's irrelevant in VB 6 because you don't write code in VB 6 in the first place if you care about parsimonious memory usage and maximum speed. There are way more advantages to creating a structure (user-defined type), or even a class, that represents the color type, and then using that in your code instead. This way, you get all the readability benefits and type-safety (which is something that you don't get in C with a
typedef
). If you want to implement theColor
type under the hood as aLong
, that's your business.