Results 1 to 4 of 4

Thread: Ctype Reloaded

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2007
    Posts
    544

    Ctype Reloaded

    I am tired of turning one html object into another. One way to do so would be to convert them to ihtmldomnode and then back

    So here we go
    vb.net Code:
    1. <Extension()>
    2.     Public Function convertTo(Of Somethingelse)(ByVal node As mshtml.IHTMLDOMNode) As Somethingelse
    3.         Return CType(node, Somethingelse)
    4.     End Function
    5.     <Extension()>
    6.     Public Function toNode(Of something)(ByVal someStuff As something) As mshtml.IHTMLDOMNode
    7.         Return CType(someStuff, mshtml.IHTMLDOMNode)
    8.     End Function
    9.     'Why Can't I do this
    10.     '<Extension()>
    11.     'Public Function CtypeReloaded(Of something, somethingelse)(ByVal someStuff As something) As somethingelse
    12.     '    Return CType(someStuff, somethingelse) 'Error  1   Value of type 'something' cannot be converted to 'somethingelse'.   \\work\c\business\fromwork\currentprojects\program\library\vb.net\internetAccessSupporter.vb    20  22  googlepages2
    13.     '    Return CType("", somethingelse)
    14.     'End Function
    15.  
    16. 'I like this one better
    17.     <Extension()>
    18.     Public Function CtypeNode(Of Something, Somethingelse)(ByVal someStuff As Something) As Somethingelse
    19.         Dim node = CType(someStuff, mshtml.IHTMLDOMNode) 'Error 1   Value of type 'something' cannot be converted to 'somethingelse'.   \\work\c\business\fromwork\currentprojects\program\library\vb.net\internetAccessSupporter.vb    20  22  googlepages2
    20.         Return CType(node, Somethingelse)
    21.         'For some reason   Return CType(someStuff, Somethingelse) direct doesn't work
    22.     End Function

  2. #2
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Ctype Reloaded

    You can define how CType should work with something and somethingelse:
    Code:
        Public Shared Widening Operator CType(ByVal x As something) As somethingelse
    
            Dim y As New somethingelse
            ...do conversion here
            Return y
    
        End Operator
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2007
    Posts
    544

    Re: Ctype Reloaded

    I need something where I can use template.

  4. #4
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Ctype Reloaded

    I already gave you a template. I will give you a template with more details. You will have to adapt it to your needs.

    Class2:
    Code:
    Public Class Class2
    
        Private x As Int16 '32767
    
        Public Property Number() As Int16
            Get
                Return x
            End Get
            Set(ByVal value As Int16)
                x = value
            End Set
        End Property
    
    End Class
    Class3:
    Code:
    Public Class Class2
    
        Private x As Int16 '32767
    
        Public Property Number() As Int16
            Get
                Return x
            End Get
            Set(ByVal value As Int16)
                x = value
            End Set
        End Property
    
    End Class
    Class1 has customized CType functions to convert from Class1 and Class2. These functions are not perfect and can cause exceptions. They are merely for demonstration:
    Code:
    Public Class Class1
    
        Private x As Int32 '2147483647 
    
        Public Property Number() As Int32
            Get
                Return x
            End Get
            Set(ByVal value As Int32)
                x = value
            End Set
        End Property
    
        Public Shared Widening Operator CType(ByVal intNumber As Class2) As Class1
            Dim sngNumber As New Class1
            sngNumber.Number = Convert.ToInt32(intNumber.Number)
            Return sngNumber
        End Operator
    
        Public Shared Narrowing Operator CType(ByVal dblNumber As Class3) As Class1
            Dim sngNumber As New Class1
            If dblNumber.Number <= 2147483647 And dblNumber.Number >= -2147483648 Then
                sngNumber.Number = Convert.ToInt32(dblNumber.Number)
            End If
            Return sngNumber
        End Operator
    
    End Class
    If you wish to text it out then create a form with 3 textboxes and two buttons. You don't have to, but it may help to add 3 labels to the textboxes. Textbox1 is the results of the conversion, the contents of class1. Textbox2 will set the contents of Class2. Ditto with 3. Button1 converts Class2 to Class1. Button2 converts Class3 to Class1.
    Form code:
    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim C1 As Class1
            Dim C2 As New Class2
    
            C2.Number = Int16.Parse(TextBox2.Text)
            C1 = CType(C2, Class1)
    
            TextBox1.Text = C1.Number.ToString
    
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    
            Dim C1 As Class1
            Dim C3 As New Class3
    
            C3.Number = Int64.Parse(TextBox3.Text)
            C1 = CType(C3, Class1)
    
            TextBox1.Text = C1.Number.ToString
    
        End Sub
    End Class
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width