有没有办法"CType"?从客体价值 [英] Is there a way to "CType" from object value
问题描述
大家好,
我陷入了一个让我发疯的问题:我正在尝试开发一组类(实际上是结构),这些类表示具有能力的基本VB.NET数据类型,例如Integer,Double,String等.设置DBNull值.
我的意思是类型(例如)_Integer的值可以是-2 ^ 16到+ 2 ^ 16-1或VBNull.Value的数字.
我已经成功编写了所有必需的运算符和方法,并且除了对象类型的赋值之外,其他所有函数都工作正常.
示例:
Hi guys,
I''m stuck on a problem that is driving me crazy: I''m trying to develop a set of classes (actually structures) that represents basic VB.NET data types like Integer, Double, String, ... with the ability to set a DBNull value.
I mean a value of type (say) _Integer can be a number from -2^16 to +2^16-1 or VBNull.Value.
I''ve successfully written all the required operators and methods and all is working just fine except the assignment from an object type.
Example:
<serializable()>
Public Structure _Integer
Public Value As Integer
Public IsNull As Boolean
Public Sub New(ByVal obj As Object)
If TypeOf(obj) Is DBNull Then
Me.Value = 0
Me.IsNull = True
Else
Me.Value = CInt(obj)
Me.IsNull = False
End If
End Sub
......
Public Shared Widening Operator CType(ByVal value As Integer) As _Integer
Return New _Integer(value)
End Operator
Public Shared Widening Operator CType(ByVal value As DBNull) As _Integer
Return New _Integer(value)
End Operator
End Structure
// SAMPLE
Dim a As _Integer = 1 //Ok
Dim o As Object = 1
Dim b As _Integer = o //Exception: Cannot Cast From Object
有人可以帮我找到一种方法来像基本类型一样初始化我的自定义类型吗?
谢谢
Can somebody pleas help me find a way to initialize my custom type just like a basic type?
Thank you
推荐答案
在拉动所有头发之前(如果还剩下; P),让我将注意力转移到绘图板上.我不确定通过创建自己的可以具有NULL
值的_Integer
想要实现什么.您是否听说过可空值类型 [
Before you pull all of your hairs (if you have any left ;P ) let me direct your attention to the drawing board. I am not sure what are you trying to achieve by creating your own_Integer
that can haveNULL
value. Have you heard of Nullable Value Types[^]? Is there any thing the Nullable Values can''t solve in your problem? I smells to be they are what you need, don''t you think? Take a closer look on it decide for yourself.
可能,您正在做Microsoft已经完成的工作,如果我没记错的话,该功能自v.3.5起可用. br/>
请参阅:
http://en.lmgtfy.com/?q=nullable+types+VB.NET [< ^ ].功能的主要用途与您的用途完全相同.
我检查了最上面的大多数搜索结果都非常相关.
我看着被coccide删除的答案.
我应该注意,报告的麻烦不是不使用.NET可为空的类型的有效借口.
您始终可以将从数据库到.NET可空类型的转换包装起来.将null替换为零也是无效的.空和零是两个不同的值.
—SA
Chances are, you''re doing the job already done by Microsoft, available since v.3.5 if I''m not mistaken.
Please see:
http://en.lmgtfy.com/?q=nullable+types+VB.NET[^]. Primary purpose of feature is exactly same as yours.
Most search results on top are quite relevant, I checked up.
I looked at the deleted Answer by coccide.
I should note that the reported hassle is not a valid excuse for not using .NET nullable types.
You can always wrap conversion from data base to .NET nullable type. Having null replaced by zero is also not valid. Null and zero are two different values.
—SA
除了Yusuf的出色建议外,您的_Integer
可能还会出现问题.
如果将其初始化为DBNull
,则IsNull
将返回True
,而Value
将返回0.
不能两者兼有.这可能不会导致任何问题.
In addition to Yusuf''s excellent advice there is a prospective problem with your_Integer
.
If it is initialized toDBNull
IsNull
will returnTrue
and yetValue
will return 0.
It cannot be both. This could cause no end of problems.
这篇关于有没有办法"CType"?从客体价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!