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:
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 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
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.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
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




Reply With Quote