简体   繁体   English

选项严格并将对象装箱到字符串

[英]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语法,则可以使用DirectCastCType 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对象的引用,而如果使用CStrToString ,则不会很明显。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Option Strict On 禁止 String 和 Object {String} 之间的隐式转换 - Option Strict On disallows implicit conversions between String and Object {String} 如何修复 Option Strict On 不允许在复制“ByRef”参数的值时从“Object”类型缩小到“String”类型 - how to fix Option Strict On disallows narrowing from type 'Object' to type 'String' in copying the value of 'ByRef' parameter VB.NET 错误消息 - “Option Strict On 不允许从 'Object' 到 'String' 的隐式转换” - VB.NET error message - “Option Strict On disallows implicit conversions from 'Object' to 'String'” 选项严格打开,设置未知对象类型的焦点 - Option Strict On, set Focus of unknown object type Option Strict On禁止操作数类型对象 - Option Strict On prohibits operands type object Option Strict ON禁止运算符&的object类型的操作数 - Option Strict ON prohibits operands of type object for operator & 将两个文本字符串与选项严格分开 - Dividing two text string with option strict on 选项严格禁止从字符串到双精度和双精度到字符串的隐式转换 - Option strict on disallows implicit conversion from string to double and double to string 没有选项的严格属性 Strict On - Strict Properties Without Option Strict On 错误:Option Strict On不允许从“对象”到“十进制”的隐式转换 - Error: Option Strict On disallows implicit conversions from 'Object' to 'Decimal'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM