|
-
Mar 23rd, 2011, 06:21 AM
#1
Thread Starter
Fanatic Member
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:
<Extension()> Public Function convertTo(Of Somethingelse)(ByVal node As mshtml.IHTMLDOMNode) As Somethingelse Return CType(node, Somethingelse) End Function <Extension()> Public Function toNode(Of something)(ByVal someStuff As something) As mshtml.IHTMLDOMNode Return CType(someStuff, mshtml.IHTMLDOMNode) End Function 'Why Can't I do this '<Extension()> 'Public Function CtypeReloaded(Of something, somethingelse)(ByVal someStuff As something) As somethingelse ' 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 ' Return CType("", somethingelse) 'End Function 'I like this one better <Extension()> Public Function CtypeNode(Of Something, Somethingelse)(ByVal someStuff As Something) As Somethingelse 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 Return CType(node, Somethingelse) 'For some reason Return CType(someStuff, Somethingelse) direct doesn't work End Function
-
Mar 23rd, 2011, 07:43 AM
#2
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
-
Mar 24th, 2011, 10:50 AM
#3
Thread Starter
Fanatic Member
Re: Ctype Reloaded
I need something where I can use template.
-
Mar 24th, 2011, 12:34 PM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|