TypeConverters

  • Thread starter Thread starter Rick Voight
  • Start date Start date
R

Rick Voight

Hi.

I have a class N with two properties.
I have a control C with a property of type N.
I wrote a Type Converter class for class N that can
convert a string into N (splits the string and assign them
to the properties).
I manage to put a control of type C on a form and update
N's value through the designer. But I can't make the
following sample code to work:
Dim MyC as new C
Dim S as String

'Class C has a property 'PN' of type N
'Class N has two String properties Prop1 & Prop2

'The next two lines works perfectly
MyC.PN.Prop1 = "Rick"
MyC.PN.Prop2 = "Voight"

'These lines don't compile, although I have a
'TypeConverter for N defined to convert a String to N
MyC.PN = "Rick Voight"
MyC.PN = CType("Rick Voight",N)


Anyone ?
 
What errors are thrown ?

Are your class member public ( if not u wont be able to set them )
 
Rick:

You are setting MyC.PN = an N. Whatever the type converter is doing, it
doesn't look like it's coming into affect here...it looks like a straight
Conversion to a Type that it can't be converted to . From the code,
although I haven't seen the class, if you can assign myC.PN to a String, and
a string doesn't have the two properties that an N does, how can it convert?
I could see if the converter was in place..but unless you have an overload
that looks like a likely culprit.

In the first one, you are assigning a String where it expects an n. Not
sure how the converter is implemented, but strings don't natively have the
same two properties that you N does.

Hopefully this helps.
 
Ok,
Here is the full code.

Total of three files:

Name.vb - The "data" class with two properties.
NameConverter.vb - The TypeConverter class for class Name
NewTextBox.vb - A control inheriting from TextBox with
additional property of type Name.


After you build it, start a new windows project,
register "NewTextBox" in your tool box and add it to a
form.

You can now change the value of MyProperty using the
designer and it will show up on your form constructor
(hence, from String -> Name -> InstanceDescriptor)
however, the simple String Assignmnet doesn't work.

Hope you can help me.

Thanks





'Name.vb
Imports System.ComponentModel
<TypeConverter(GetType(NameConverter))> _
Public Class Name

Public Sub New()
End Sub

Public Sub New(ByVal FirstName As String, ByVal
LastName As String)
Me.New()
Me.Last = LastName
Me.First = FirstName
End Sub

Private _Last As String
Public Property Last() As String
Get
Return _Last
End Get
Set(ByVal Value As String)
_Last = Value
End Set
End Property

Private _First As String
Public Property First() As String
Get
Return _First
End Get
Set(ByVal Value As String)
_First = Value
End Set
End Property
End Class


'NameConverter.vb
Imports System
Imports System.ComponentModel
Imports System.Globalization

Public Class NameConverter
Inherits TypeConverter

Public Overloads Overrides Function CanConvertFrom
(ByVal context As ITypeDescriptorContext, ByVal sourceType
As Type) As Boolean
If sourceType Is GetType(String) Then
Return True
End If
Return MyBase.CanConvertFrom(context, sourceType)
End Function

Public Overloads Overrides Function ConvertFrom(ByVal
context As ITypeDescriptorContext, ByVal culture As
CultureInfo, ByVal value As Object) As Object
If TypeOf value Is String Then
Dim v As String() = CStr(value).Split(New Char
() {" "c})
Return New Name(v(0), v(1))
End If
Return MyBase.ConvertFrom(context, culture, value)
End Function


Public Overloads Overrides Function CanConvertTo(ByVal
context As System.ComponentModel.ITypeDescriptorContext,
ByVal destinationType As System.Type) As Boolean
If destinationType Is GetType
(System.ComponentModel.Design.Serialization.InstanceDescrip
tor) Then
Return True
End If
If destinationType Is GetType(String) Then
Return True
End If
Return MyBase.CanConvertTo(context,
destinationType)
End Function

Public Overloads Overrides Function ConvertTo(ByVal
context As ITypeDescriptorContext, ByVal culture As
CultureInfo, ByVal value As Object, ByVal destinationType
As Type) As Object
If destinationType Is GetType(String) Then
Return CType(value, Name).First & " " & CType
(value, Name).Last
End If

If destinationType Is GetType
(System.ComponentModel.Design.Serialization.InstanceDescrip
tor) Then
'This will generate a call using New Name
(First,Last)
Dim ci As System.Reflection.ConstructorInfo =
GetType(Name).GetConstructor(New Type() {GetType(String),
GetType(String)})
Dim t As Name = CType(value, Name)
Return New
System.ComponentModel.Design.Serialization.InstanceDescript
or(ci, New Object() {t.First, t.Last})
End If
Return MyBase.ConvertTo(context, culture, value,
destinationType)
End Function
End Class



'NewTextBox.vb
Public Class NewTextBox
Inherits System.Windows.Forms.TextBox

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form
Designer.
InitializeComponent()

'Add any initialization after the
InitializeComponent() call
_MyProperty = New Name()
Dim s As String = "Liron Kedem"
'_MyProperty = s
End Sub

'UserControl1 overrides dispose to clean up the
component list.
Protected Overloads Overrides Sub Dispose(ByVal
disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the
Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
'
'NewTextBox
'
Me.Name = "NewTextBox"

End Sub

#End Region

Private _MyProperty As Name

Public Property MyProperty() As Name
Get
Return _MyProperty
End Get
Set(ByVal Value As Name)
_MyProperty = Value
End Set
End Property

End Class
 
Back
Top