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

本文共 1899 字,大约阅读时间需要 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: 330987132 | Go:217696290 | Python:336880185 | 做人要厚道,转载请注明出处!http://www.cnblogs.com/sunshine-anycall/archive/2010/12/22/1913789.html
你可能感兴趣的文章
wdOS系统下源码编译安装LAMP环境(linux+apache+php+mysql)
查看>>
iOS 绕过相册权限漏洞
查看>>
我的友情链接
查看>>
XP 安装ORACLE
查看>>
八、 vSphere 6.7 U1(八):分布式交换机配置(vMotion迁移网段)
查看>>
[转载] 中华典故故事(孙刚)——19 万岁
查看>>
修改hosts文件里面的主机名,oralce asm无法启动
查看>>
Maven学习总结(十)——使用Maven编译项目gbk的不可映射问题
查看>>
php5编译安装常见错误和解决办法集锦
查看>>
Linux远程访问及控制
查看>>
MongoDB实战系列之五:mongodb的分片配置
查看>>
Unable to determine local host from URL REPOSITORY_URL=http://
查看>>
Java Tomcat SSL 服务端/客户端双向认证(二)
查看>>
java基础(1)
查看>>
ORACLE配置,修改tnsnames.ora文件实例
查看>>
用户无法在输入框中键入数字
查看>>
Workstation服务无法启动导致无法访问文件服务器
查看>>
Gradle:Basic Project
查看>>
.Net组件程序设计之远程调用(二)
查看>>
ant中文教程
查看>>