VB.NET 类型转换列表视图标记对象
在 C# 中我会做类似的事情:
mytype val = (mytype)mylistview.SelectedItems(0).Tag;
我怎样才能在 VB.NET 中做同样的事情?
In C# i would do something like:
mytype val =
(mytype)mylistview.SelectedItems(0).Tag;
how can I do the same thing in VB.NET?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的 VB 很糟糕,但我认为应该是:
或
DirectCast
不执行任何其他转换 - 包括 (IIRC) 用户指定的转换,而
CType
将比 C# 中的强制转换执行更多次转换在这种特殊情况下,我认为
DirectCast
可能就是您所追求的,因为它应该只是一个引用转换。My VB sucks, but I think it would be:
or
DirectCast
doesn't perform any other conversions - including (IIRC) user-specified conversions, whereasCType
will perform more conversions than a cast in C# wouldIn this particular case, I think
DirectCast
is probably what you're after, as it should be just a reference conversion.对于绝大多数情况,
CType
运算符将在此处给出正确的行为。然而,并非在所有情况下都是如此。原因是 C# 强制转换运算符和 VB 中的等效运算符之间不存在 1-1 映射。 C# 强制转换运算符支持 CLR 和用户定义的转换运算符。
VB 的两个主要转换运算符是 DirectCast 和 CType。 DirectCast 仅支持运行时转换,并且会丢失用户定义的转换。 CType 支持运行时和用户定义的转换。但它也支持词法转换(例如字符串文字“123”到
Integer
类型)。因此,它将捕获 C# 强制转换运算符执行的所有操作,但还包括更多内容。For the vast majority of cases the
CType
operator will give the correct behavior here.However this is not true in every case. The reason why is that there is no 1-1 mapping between the C# cast operator and an equivalent operator in VB. The C# cast operator supports both CLR and user defined conversion operators.
VB's two main casting operators are DirectCast and CType. DirectCast supports runtime conversions only and will miss user defined ones. CType supports runtime and user defined conversions. But it also supports lexical conversions (such as the string literal "123" to an
Integer
type). So it will catch everything a C# cast operator does but also include more.不确定我是对的,不知道你到底想做什么,但一般语法是:
Not sure I'm right not knowing what exactly you're trying to do but general syntax would be: