How to: Define a Conversion Operator [msdn]

简介:

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 CType Function procedure within the class or structure. All conversion procedures must be Public Shared, and each one must specify either Widening or Narrowing.

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 digit
Private dig As Byte
Public Sub New(ByVal b As Byte)
If (b < 0 OrElse b > 9) Then Throw New _
System.ArgumentException("Argument outside range for Byte")
Me.dig = b
End Sub
Public Shared Widening Operator CType(ByVal d As digit) As Byte
Return d.dig
End Operator
Public Shared Narrowing Operator CType(ByVal b As Byte) As digit
Return New digit(b)
End Operator
End 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))
Try
Dim d4 As digit
d4 = CType(CType(d1, Byte) + CType(d2, Byte), digit)
Catch e4 As System.Exception
s &= vbCrLf & "4 + 7 generates " & """" & e4.Message & """"
End Try
Try
Dim d5 As digit = CType(CByte(10), digit)
Catch e5 As System.Exception
s &= vbCrLf & "Initial 10 generates " & """" & e5.Message & """"
End Try
MsgBox(s)
End Sub

 

欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 572064792 | Nodejs:329118122 做人要厚道,转载请注明出处!







本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/sunshine-anycall/archive/2010/12/22/1913789.html ,如需转载请自行联系原作者


相关文章
|
3天前
|
人工智能 安全 机器人
【C++】dynamic_cast基本用法(详细讲解)
【C++】dynamic_cast基本用法(详细讲解)
|
5月前
|
小程序
小程序 define is not defined
小程序 define is not defined
49 0
|
8月前
#define的用法
在程序中扩展#define定义符号和宏时,需要涉及几个步骤。 1. 在调用宏时,首先对参数进行检查,看看是否包含任何由#define定义的符号。如果是,它们首先 被替换。 2. 替换文本随后被插入到程序中原来文本的位置。对于宏,参数名被他们的值所替换。 3. 最后,再次对结果文件进行扫描,看看它是否包含任何由#define定义的符号。如果是,就重复上述处理过程。
52 0
|
11月前
|
关系型数据库 MySQL C++
类型收窄 error C2397: conversion from ‘const int‘ to ‘char‘ requires a narrowing conversion
类型收窄 error C2397: conversion from ‘const int‘ to ‘char‘ requires a narrowing conversion
119 0
GLib-CRITICAL : g_variant_get_uint32: assertion ‘g_variant_is_of_type (value, G_VARIANT_TYPE_UINT32)
GLib-CRITICAL : g_variant_get_uint32: assertion ‘g_variant_is_of_type (value, G_VARIANT_TYPE_UINT32)
118 0
|
编译器 C++
Effective C++条款 02:尽量以 const, enum, inline 替换 #define
Effective C++条款 02:尽量以 const, enum, inline 替换 #define
111 0
|
编译器
USES_CONVERSION宏定义
USES_CONVERSION宏定义
303 0
|
C++
c/c++ define用法
define,无参宏定义的一般形式为:#define 标识符 字符串 外文名 define 词条范围 计算机专业用语 无参一般形式 #define 标识符 字符串 带参一般形式 #define 宏...
2232 0
获得ABAP report里定义的所有变量及type - GET_GLOBAL_SYMBOLS
获得ABAP report里定义的所有变量及type - GET_GLOBAL_SYMBOLS
117 0