欢迎光临
我们一直在努力

VB.NET特性之FieldOffset特性-.NET教程,VB.Net语言

建站超值云服务器,限时71元/月

vb.net特性

—–fieldoffset特性

在选择显示布局的时候,结构中的所有变量的定义必须包含fieldoffset特性。这个特性指定了距结构开始处的距离(以字节位单位)。

imports system.runtime.interopservices

<structlayout(layoutkind.explicit)> _

structure test

<fieldoffset(0)>dim red as byte

<fieldoffset(1)>dim green as byte

<fieldoffset(2)>dim blue as byte

<fieldoffset(3)>dim alpha as byte

end structure

structlayout特性与fieldoffset特性可以实现联合(union)。联合(union)已经被多种语言(如 c和c++)采用,但是vb却不具备这一语言特性。联合(union)是一种可以使得结构中的两个或多个元素在内存中重叠,以及使用不同的名称来指示同一内存位置。

在.net中,联合(union)的关键在于支持显示结构布局。

如:

imports system.runtime.interopservices

<structlayout(layoutkind.explicit)> _

structure test

<fieldoffset(0)>dim red as byte

<fieldoffset(1)>dim green as byte

<fieldoffset(2)>dim blue as byte

<fieldoffset(3)>dim alpha as byte

<fieldoffset(0)>dim value as integer

end structure

则这些元素在内存中的位置,如图:

这样就可以通过value 字段将4个字节作为一个整体进行访问。

拆分

dim rgb as test

rgb.value=&h112233 1122867

console.write("red={0},green={1},blue={2}",rgb.red,rgb.green,rgb.blue)

输出如:

‘合并

rgb.red=51

rgb.green=34

rgb.blue=17

console.write(rgb.value)

输出如:

这样就可以解决很多转换的工作,而且比使用数学运算符更快!

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » VB.NET特性之FieldOffset特性-.NET教程,VB.Net语言
分享到: 更多 (0)

相关推荐

  • 暂无文章