How to do this convertion?

generic , type , convertion Mar del Plata, Argentina
  • 12 years ago
    When the exception is thrown, the Watch window with this watch: [COLOR="Red"]CType(Data.Value, IEquatable(Of Tipo))[/COLOR] converts from string to IEquatable(of Integer) without problem, although converts to a String "876765" and also the Watch window window with this watch: [COLOR="Red"]CType(CType(Data.Value, IEquatable(Of Tipo)), Tipo)[/COLOR] Converts string [to IEquatable(of String) ] to Integer without problem, so why the exception is thrown? how to fix it? I cannot assume that Data.Value is numeric, although if so, I never call the routine without checking previously that is numeric. ____________________________________ Option Strict On [COLOR="SeaGreen"]'Also don't work with Option Strict off[/COLOR] Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim IntegerString As New [COLOR="DimGray"]GenericData(Of String)[/COLOR] IntegerString.Value = "876765" Dim IntegerContainer As New MyGenericContainer(Of Integer) IntegerContainer.Assign(Of String)(IntegerString) End Sub [COLOR="DimGray"]Class GenericData(Of Tipe As IEquatable(Of Tipe)) Public Value As Tipe End Class[/COLOR] Friend Class MyGenericContainer(Of Tipo As IEquatable(Of Tipo)) Private Variable As Tipo Friend Sub Assign(Of Tipe As IEquatable(Of Tipe))(ByRef Data As GenericData(Of Tipe)) Try Variable = [COLOR="Red"]CType(CType(Data.Value, IEquatable(Of Tipo)), Tipo)[/COLOR] Catch ex As Exception Throw New Exception("Cannot convert """ & Data.Value.GetType.ToString & """ to """ & Variable.GetType.ToString & """") End Try End Sub End Class End Class
  • 12 years ago
    The code looks Ugly. Since It looks like it cannot be edited, I will try post the code again: Option Strict On 'Also don't work with Option Strict off Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim IntegerString As New GenericData(Of String) IntegerString.Value = "876765" Dim IntegerContainer As New MyGenericContainer(Of Integer) IntegerContainer.Assign(Of String)(IntegerString) End Sub Class GenericData(Of Tipe As IEquatable(Of Tipe)) Public Value As Tipe End Class Friend Class MyGenericContainer(Of Tipo As IEquatable(Of Tipo)) Private Variable As Tipo Friend Sub Assign(Of Tipe As IEquatable(Of Tipe))(ByRef Data As GenericData(Of Tipe)) Try Variable = CType(CType(Data.Value, IEquatable(Of Tipo)), Tipo) Catch ex As Exception Throw New Exception("Cannot convert """ & Data.Value.GetType.ToString & """ to """ & Variable.GetType.ToString & """") End Try End Sub End Class End Class

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.” - Rick Osborne