涕淌居

牛~冬~@~瘋~牛~涕~淌~の~涕~淌~居~~GnuDoyng's Web Log~~~Contact me~niu_dong@msn.com~ICQ:277276843~QQ:123930135~

原创 [译]Visual Basic 2005在语言上的增强(十一)无符号类型收藏

Visual Basic 2005完全地支持了无符号类型。这些无符号类型名包括:
• SByte
• UShort
• UInteger
• ULong

除了只能存储非负整数以外,无符号类型和常规类型的工作原理是一样的。

当Visual Basic调用Win32的API、需要传入无符号类型的参数时,无符号类型就极为有用了。下面这段代码演示了如何调用Windows的MessageBox API:
Public Enum WindowsMessageResult As UInteger
    OK = 1
    Cancel = 8
End Enum

Private Const MB_OK As UInteger = 0
Private Const MB_ICONEXCLAMATION As UInteger = &H30
Private Const MB_OPTIONS As UInteger = MB_OK Or MB_ICONEXCLAMATION

Private Declare Auto Function Win_MB Lib _
    "user32.dll" Alias "MessageBox" _
    (ByVal hWnd As Integer, ByVal lpText As String, _
     ByVal lpCaption As String, ByVal uType As UInteger) _
     As UInteger

Public Function MessageThroughWindows(ByVal message As String, _
    ByVal caption As String) As WindowsMessageResult
    Dim r As UInteger = Win_MB(0, message, caption, MB_OPTIONS)
    Return CType(r, WindowsMessageResult)
End Function

你看,在Win_MB函数声明中的最后一个参数以及返回值都是UInteger类型的。

如果你想存储超过了Integer范围的数值,那么无符号类型也可以替你节省空间。Integer类型占用四字节的内存并且只能存储不超过2,147,483,648的正数或负数。UInteger类型也只占用四字节内存,却可以存储从零到4,294,967,295的数值。如果没有了无符号类型,你又不得不使用占用了八字节内存的Long来存储这么大的数值了。


@原文在此供网友们参考@

Unsigned Types

Visual Basic 2005 has full support for unsigned types. The unsigned type names are:
• SByte
• UShort
• UInteger
• ULong

The unsigned types work as regular types except the unsigned type can store only positive integers.

Unsigned types are most useful in Visual Basic for making Win32 API calls that take unsigned types as parameters. The following code illustrates a Windows MessageBox API call:
Public Enum WindowsMessageResult As UInteger
    OK = 1
    Cancel = 8
End Enum

Private Const MB_OK As UInteger = 0
Private Const MB_ICONEXCLAMATION As UInteger = &H30
Private Const MB_OPTIONS As UInteger = MB_OK Or MB_ICONEXCLAMATION

Private Declare Auto Function Win_MB Lib _
    "user32.dll" Alias "MessageBox" _
    (ByVal hWnd As Integer, ByVal lpText As String, _
     ByVal lpCaption As String, ByVal uType As UInteger) _
     As UInteger

Public Function MessageThroughWindows(ByVal message As String, _
    ByVal caption As String) As WindowsMessageResult
    Dim r As UInteger = Win_MB(0, message, caption, MB_OPTIONS)
    Return CType(r, WindowsMessageResult)
End Function

Notice the last parameter and return type on the Win_MB declaration are both UInteger types.

Unsigned types can also help you conserve memory if you need to store values larger than an Integer. The Integer type uses four bytes of memory and can store positive and negative values up to 2,147,483,648. The UInteger type also uses four bytes and can store a value between zero and 4,294,967,295. Without an unsigned type, you'd need to use a Long taking eight bytes of memory to store values this large.

发表于 @ 2005年01月13日 07:54:00|评论(loading...)

新一篇: [译]Visual Basic 2005在语言上的增强(十二)默认实例和编译器警告 | 旧一篇: [译]Visual Basic 2005在语言上的增强(十)应用程序级别事件

用户操作
[即时聊天] [发私信] [加为好友]
疯牛涕淌
订阅我的博客
XML聚合  FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
疯牛涕淌的公告
123930135~有蝦米代誌請Q我哦! 本blog已搬家...
文章分类
收藏
百科全書
Britannica
Encyclopedia Mythica
Encyclopedia of Philosophy
Encyclopedia.com
MSN Encarta
WIKIPEDIA
大師之家
Code/Tea/Etc...
Mind View
Panopticon Central
Robert Green's VB Blog
Rockford Lhotka
The Mountain of Worthless Information
The VB Team
VB.NET Expert
侯捷網站
話語資源
American Dialects
Ethnologue
Glossary
IPA HTML Entities
Lexicon of Linguistics
The Linguistic Society of America
Übungen Für Deutsch
Word 2 Word
WordNet
Yamada Language Guides
yourDictionary
技術社區
.NET Framework Windows Forms
ASP.NET Heaven
ASP.NET Web
C# Corner
C# Friends
C# Help
Code Guru
Dev Guru
Developer Fusion
DotNet Zone
Got DotNet
MIT開放式課程網頁
Most Valuable Professional
MSDN
SuperSite for Windows
The Code Project
VB Forums
VB Info Zine
VB.NET Heaven
VBCity
中國 Dot Net Nuke
微軟中國社區
外國朋友
Bram Blogs
Дневник elishena
Добре дошли, приятели!
Заметки обалдевшей от счастья матери
友情鏈接
Amoeba原蟲
kgd jeyp
Let's Eat Noodles at Last
Nature Is Restless
YOYO Forever
三生石坊
去日留痕
咖啡愛我
夢想飛翔
天高任我飛
奔騰不息
宜軒品
寧靜*海豚灣
形而上的憂傷
我愛茉莉
我的生活
既生甲何生乙
明天的寂寞
沫言沫語
異域星空
裝配中的腦袋
閩中龍
阿貴
風一代
與我有關
Linguistics Paradise
UÕH UỎNG SANG
閩東語論壇
只是路過
安妮寶貝
微微微笑
存档
软件项目交易
Csdn Blog version 3.1a
Copyright © 疯牛涕淌