博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
How to: Define a Conversion Operator [msdn]
阅读量:7067 次
发布时间:2019-06-28

本文共 1919 字,大约阅读时间需要 6 分钟。

because of my project, learned something about VB.NET. and now by chance, find this stuff. it's pretty cool.

Visual Studio 2005

 

If you have defined a class or structure, you can define a type conversion operator between the type of your class or structure and another data type (such as IntegerDouble, or String).

Define the type conversion as a  procedure within the class or structure. All conversion procedures must be Public Shared, and each one must specify either  or .

Defining an operator on a class or structure is also called overloading the operator.

Example

The following example defines conversion operators between a structure called digit and a Byte.

Public Structure digitPrivate dig As BytePublic Sub New(ByVal b As Byte)If (b < 0 OrElse b > 9) Then Throw New _System.ArgumentException("Argument outside range for Byte")Me.dig = bEnd SubPublic Shared Widening Operator CType(ByVal d As digit) As ByteReturn d.digEnd OperatorPublic Shared Narrowing Operator CType(ByVal b As Byte) As digitReturn New digit(b)End OperatorEnd Structure

You can test the structure digit with the following code.

Public Sub consumeDigit()Dim d1 As New digit(4)Dim d2 As New digit(7)Dim d3 As digit = CType(CByte(3), digit)Dim s As String = "Initial 4 generates " & CStr(CType(d1, Byte)) _& vbCrLf & "Initial 7 generates " & CStr(CType(d2, Byte)) _& vbCrLf & "Converted 3 generates " & CStr(CType(d3, Byte))TryDim d4 As digitd4 = CType(CType(d1, Byte) + CType(d2, Byte), digit)Catch e4 As System.Exceptions &= vbCrLf & "4 + 7 generates " & """" & e4.Message & """"End TryTryDim d5 As digit = CType(CByte(10), digit)Catch e5 As System.Exceptions &= vbCrLf & "Initial 10 generates " & """" & e5.Message & """"End TryMsgBox(s)End Sub

 

欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 572064792 | Nodejs:329118122 做人要厚道,转载请注明出处!
本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/sunshine-anycall/archive/2010/12/22/1913789.html
,如需转载请自行联系原作者
你可能感兴趣的文章
Cointext无需联网秒转BCH,但也要理性看待不足
查看>>
写给 Android 开发的小程序布局指南,Flex 布局!
查看>>
ReactNative之AsyncStorage本地存储
查看>>
手把手教你撸一个 Webpack Loader
查看>>
冒泡和快速排序
查看>>
每天阅读一个 npm 模块(6)- pify
查看>>
MySQL索引
查看>>
HeyUI组件库12月更新日志:Clipboard复制剪切板,Avatar头像
查看>>
十大监控工具,值得一试
查看>>
ES6 -> Javascript的类与继承在Babel的实现
查看>>
走进mysql基础
查看>>
使用Rollup打包JavaScript
查看>>
推荐系统与协同过滤、奇异值分解
查看>>
TypeScript 学习(三)
查看>>
小猪的Python学习之旅 —— 20.抓取Gank.io所有数据存储到MySQL中
查看>>
从小数学就不及格的我,竟然用极坐标系表白了我的女神!(附代码)
查看>>
iOS远程hot patch的优点和风险
查看>>
Android uncovers master-key 漏洞分析
查看>>
常用操作命令使用总结
查看>>
单元测试工具 TestNG 使用
查看>>