简体   繁体   English

Option Strict On 禁止 String 和 Object {String} 之间的隐式转换

[英]Option Strict On disallows implicit conversions between String and Object {String}

I am trying to clean my ways as a beginner programmer by using Option Strict On .我正在尝试通过使用Option Strict On来清理我作为初学者程序员的方式。 I managed to clean all errors except this one.我设法清除了除此之外的所有错误。
I am using the Tag property of a ToolStrip for some text information.我正在使用 ToolStrip 的 Tag 属性来获取一些文本信息。 Clicking on the ToolStrip, I need to remember the value of the Tag in a string and change the value of that Tag.单击 ToolStrip,我需要记住字符串中 Tag 的值并更改该 Tag 的值。

How can I convert the Object {String} sender.tag to a String and the String myString and an Object {String} ?如何将Object {String} sender.tag 转换为String以及String myString 和Object {String}

Private Sub ToolStrip_ItemClicked(sender As Object, e As ToolStripItemClickedEventArgs) Handles ToolStrip.ItemClicked
    Dim myString As String = sender.tag
    sender.tag = "It is selected"
    'more code...
End Sub

Edit: see here a screenshot of the relevant part of the code:编辑:请参见此处代码相关部分的屏幕截图:

在此处输入图像描述

If you want to read the Tag Property of a ToolstripItem , you need to read the Tag of the Item selected.如果要读取ToolstripItem的 Tag 属性,则需要读取所选项目的 Tag。 This is provided by the e.ClickedItem property of the ToolStripItemClickedEventArgs object:这是由ToolStripItemClickedEventArgs object 的e.ClickedItem属性提供的:

Gets the item that was clicked on the ToolStrip.获取在 ToolStrip 上单击的项目。

If you want to read the Tag property of the ToolStrip this Item belongs to, cast sender to ToolStrip (or Control, the base class this property belongs to) and cast its Tag property to String.如果要读取此 Item 所属的 ToolStrip 的 Tag 属性,请将sender转换为 ToolStrip(或 Control,此属性所属的基础 class)并将其 Tag 属性转换为 String。

Note: when you double-click a ToolStrip Control, an ItemClicked handler is created because this is the default event of that Control .注意:当您双击 ToolStrip 控件时,会创建一个ItemClicked处理程序,因为这是该控件的默认事件 The event is related to the ToolStrip object, so the sender object in the event handler will be the reference of the ToolStrip instance that raised the event.该事件与 ToolStrip object 相关,因此事件处理程序中的sender object 将是引发事件的 ToolStrip 实例的引用。

The Tag Property is of Type Object , that's why you need to cast that, too. Tag 属性的类型为Object ,这也是您需要转换它的原因。

Private Sub ToolStrip_ItemClicked(sender As Object, e As ToolStripItemClickedEventArgs) Handles ToolStrip.ItemClicked
    Dim itemTagAsString As String = e.ClickedItem.Tag?.ToString()

    Dim toolStripTagAsString = DirectCast(sender, ToolStrip).Tag?.ToString()
    ' Or, cast to Control, the Tag Property belongs to the base class
    Dim toolStripTagAsString = DirectCast(sender, Control).Tag?.ToString()
End Sub

Check the Type of itemTtag in these examples (with Option Infer On ):在这些示例中检查itemTtag的类型(使用Option Infer On ):

Dim itemTtag = e.ClickedItem.Tag
Dim itemTtag = e.ClickedItem.Tag?.ToString()

So it's more clear why this is not a valid statement:所以更清楚为什么这不是一个有效的陈述:

Dim itemTtag As String = e.ClickedItem.Tag

Note: the null conditional operator ( ? , aka Elvis), is used here because the Tag could be null ( Nothing ).注意:此处使用null 条件运算符? ,又名 Elvis),因为标签可能是 null( Nothing )。 If it is, you won't get an exception, just an empty string.如果是,你不会得到异常,只是一个空字符串。

Good that you have titled your question generically.很好,你已经为你的问题命名。 And you need a generic answer.你需要一个通用的答案。

Option Strict On is a good thing. Option Strict On是一件好事。 It makes harder to code but performance at runtime will increase because there will be less implicit data type conversions.它使编码变得更加困难,但运行时的性能会提高,因为隐式数据类型转换会减少。

Lets take your code ...sender As Object... and ...sender.tag.. This is a typical thing in .net.让我们将您的代码...sender As Object......sender.tag..这是 .net 中的典型内容。 Often you see a parameter of type object , which means, any data type can be passed.您经常会看到object类型的参数,这意味着可以传递任何数据类型。 Object does not have all the properties and methods defined that belong to that data type. Object 没有定义属于该数据类型的所有属性和方法。

For example例如

Dim oTxt as object = new TextBox()

oTxt will not automatically have property Text . oTxt不会自动具有属性Text You need to cast .你需要cast When you know 100% the object type, do当您 100% 了解 object 类型时,请执行

dim str as string = DirectCast(oTxt, TextBox).Text

But sometimes you don't know what Type is in your object .但有时您不知道object中的Type是什么。 In this case you try casting and then check for null在这种情况下,您尝试投射,然后检查null

dim txtBx as TextBox = TryCast(oTxt, TextBox)
if txtBx IsNot Nothing Then str = txtBx.Text

Your real problem is that you need to understand type casting.您真正的问题是您需要了解类型转换。 You should cast it explicitly even if you don't have Options Strinct On because when you do x = sender.tag implicitly, you really using a late binding , which is bad for performance.即使您没有Options Strinct On ,您也应该显式地转换它,因为当您隐式执行x = sender.tag时,您实际上使用的是后期绑定,这对性能不利。 And this also opens doors for potential runtime errors.这也为潜在的运行时错误打开了大门。

Your topics of research should be: type casting, late binding, boxing/unboxing你的研究主题应该是:类型转换、后期绑定、装箱/拆箱

暂无
暂无

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

相关问题 Option Strict On不允许从'String'到'Boolean'的隐式转换 - Option Strict On disallows implicit conversions from 'String' to 'Boolean' Option Strict On 不允许从“String”到“Double”的隐式转换 - Option Strict On disallows implicit conversions from 'String' to 'Double' VB.NET 错误消息 - “Option Strict On 不允许从 'Object' 到 'String' 的隐式转换” - VB.NET error message - “Option Strict On disallows implicit conversions from 'Object' to 'String'” 严格的选项不允许在“布尔值”之间进行隐式转换 在SQL语句中布尔 - option strict on disallows implicit conversions between 'boolean?' to boolean in SQL statement 错误:Option Strict On不允许从“对象”到“十进制”的隐式转换 - Error: Option Strict On disallows implicit conversions from 'Object' to 'Decimal' Option Strict On 禁止从 'String' 到 'Char' VB.NET 的隐式转换 - Option Strict On disallows implicit conversions from 'String ' to 'Char' VB.NET “Option Strict On禁止隐式转换”执行不一致 - “Option Strict On disallows implicit conversions” inconsistent enforcement 选项严格禁止从字符串到双精度和双精度到字符串的隐式转换 - Option strict on disallows implicit conversion from string to double and double to string Option Strict On不允许从'ADODB.Recordset'到'ADODB.Recordset'的隐式转换 - Option Strict On disallows implicit conversions from 'ADODB.Recordset' to 'ADODB.Recordset' Option Strict On不允许从'System.Drawing.Point'到'System.Drawing.Size'的隐式转换 - Option Strict On disallows implicit conversions from 'System.Drawing.Point' to 'System.Drawing.Size'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM