Search code examples
c#vb.netlistviewitemcasting

VB.NET typecasting a listview tag object


In C# i would do something like:

mytype val = (mytype)mylistview.SelectedItems(0).Tag;

how can I do the same thing in VB.NET?


Solution

  • My VB sucks, but I think it would be:

    Dim val as MyType = CType(mylistview.SelectedItems(0).Tag, MyType)
    

    or

    Dim val as MyType = DirectCast(mylistview.SelectedItems(0).Tag, MyType)
    

    DirectCast doesn't perform any other conversions - including (IIRC) user-specified conversions, whereas CType will perform more conversions than a cast in C# would

    In this particular case, I think DirectCast is probably what you're after, as it should be just a reference conversion.