[英]Option Strict and Unboxing Object to String
I have a treeview where some of the treenodes have a string saved into their tag object and some of the tags are left as nothing. 我有一个treeview,其中一些treenodes已将字符串保存到其标签对象中,而某些标签则为空。 Later on I want to use the strings for something, in the nodes where they exist.
稍后,我想在它们存在的节点中将字符串用于某些东西。
For Each tn As TreeNode In TreeView1.Nodes
If Not String.IsNullOrWhiteSpace(tn.Tag) Then
Call DoTagStringStuff(tn.Tag)
End If
Next tn
This worked fine until I needed to turn on option strict to make my code compatible with a co-workers project. 在我需要严格启用选项以使我的代码与同事项目兼容之前,此方法一直有效。 I'm a bit confused about how to best unbox the string from the treenode.tag object.
我对如何最好地从treenode.tag对象中解包字符串感到困惑。
The error popup suggest using CStr
, but I was under the impression that the CStr
function was only in VB.net as a throwback to VB6, and really shouldn't be used for new code. 错误弹出窗口建议使用
CStr
,但是我给人的印象是CStr
函数仅在VB.net中作为对VB6的回溯,实际上不应该用于新代码。 If I try tn.Tag.toString
in the above code, I get an error at runtime when it fails to compute Nothing.toString
. 如果我在上面的代码中尝试
tn.Tag.toString
,则在运行Nothing.toString
失败时会在运行时出现错误。
What is the right way to fix this? 解决此问题的正确方法是什么? Should I even be using the tag object to hold string values in the first place, or is there a better treenode property that wouldn't require unboxing I can use for this?
我应该首先使用标记对象来保存字符串值,还是有一个更好的treenode属性不需要拆箱呢?
Edit: I think perhaps this would be correct? 编辑:我认为这可能是正确的吗?
For Each tn As TreeNode In theNode.Nodes
If tn.Tag IsNot Nothing Then
Call DoTagStringStuff(DirectCast(tn.Tag, String))
End If
Next tn
Except I'm not checking for an empty or only whitespace string anymore. 除非我不再检查空字符串或仅空白字符串。
I'm a bit confused about how to best unbox the string from the treenode.tag object.
我对如何最好地从treenode.tag对象中解包字符串感到困惑。
First off, there is no boxing or unboxing involved here. 首先,这里没有装箱或拆箱。 Since
String
is a class, an Object
variable can directly hold a reference to a String
without boxing. 由于
String
是一个类,因此Object
变量可以直接保存对String
的引用而无需装箱。 Boxing occurs when you store a value type ( Structure
) within an Object
variable. 当您在
Object
变量中存储值类型( Structure
)时,将发生装箱。
That being said - to extract the value, since you know you're always storing a String
, you can use DirectCast
to pull out the value: 话虽如此-提取值,因为您知道总是存储
String
,因此可以使用DirectCast
提取值:
For Each tn As TreeNode In TreeView1.Nodes
Dim tag = DirectCast(tn.Tag, String)
If Not String.IsNullOrWhiteSpace(tag) Then
Call DoTagStringStuff(tag)
End If
Next tn
If other types could potentially be stored in the Tag
, or if Tag
may be left empty, then other conversions may be appropriate. 如果其他类型可能存储在
Tag
,或者Tag
可能留空,则其他转换可能是合适的。
Using CStr
to cast/convert the tag to a string is totally legitimate: 使用
CStr
将标记CStr
转换/转换为字符串是完全合法的:
DoTagStringStuff(CStr(tn.Tag))
If you don't care for the CStr
syntax, you can use either DirectCast
or CType
, instead. 如果您不关心
CStr
语法,则可以使用DirectCast
或CType
。 CStr
is essentially just shorthand for CType(x, String)
. CStr
本质上只是CType(x, String)
简写。 While CStr
did exist in VB6, it is not considered out-dated to use it. 尽管
CStr
在VB6中确实存在,但使用它并不认为已过时。 It is still considered to be a function of the core VB.NET language. 它仍然被认为是核心VB.NET语言的功能。
Technically, since you know that the property references a String
object, the best choice would actually be DirectCast
: 从技术上讲,由于您知道该属性引用了
String
对象,因此最佳选择实际上是DirectCast
:
DoTagStringStuff(DirectCast(tn.Tag, String))
DirectCast
is slightly more efficient when you don't intend any conversion of the value to take place. 当您不打算进行任何值的转换时,
DirectCast
效率会稍高一些。 As a side benefit, it's more self-documenting. 附带的好处是,它具有更多的自我记录功能。 Anyone who reads that line will know that the
Tag
is a reference to a String
object, whereas if you used CStr
or ToString
, that wouldn't be obvious. 读过该行的任何人都将知道
Tag
是对String
对象的引用,而如果使用CStr
或ToString
,则不会很明显。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.