How to CType() dynamically ? 
Author Message
 How to CType() dynamically ?

I'm working on a form editor a.k.a like in Visual Studio and sharpdevelop.
I save the forms out as xml mainly using to the <code>.ToString
</code>methods and then try to convert them back on load.
I thought this would be the way to do it:-

Dim PInfo As PropertyInfo = ty.GetProperty(Nde.Name)
  If Not PInfo Is Nothing Then
    Dim t As Type = PInfo.GetType()
    PInfo.SetValue(ctrl, (CType(Nde.Value, PInfo.GetType())), Nothing)
  End If

but CType and DirectCast do not accept PInfo.GetType(). They demand a hand
coded type e.g. CType(Value, Integer).  Does anybody know of a way to do
this?

I look for the TagName via reflection :- Dim PInfo As PropertyInfo =
ty.GetProperty(Nde.Name) and it returns fine, so it's found the property.
But how do I then convert the value to the correct Type so I can set the
property value?

Demo XML:
<VISIBLE>True</VISIBLE>
<Text>Caption :</Text>
<TextAlign>1</TextAlign>



Sun, 17 Jul 2005 21:59:48 GMT  
 How to CType() dynamically ?
This cannot be done using CType. There have been several threads on this
before. If you want to search via Google you'll get more info on why not.
The best way to do this is to write your own function that uses a Select
Case to do the conversion based on the type.


Quote:
> I'm working on a form editor a.k.a like in Visual Studio and sharpdevelop.
> I save the forms out as xml mainly using to the <code>.ToString
> </code>methods and then try to convert them back on load.
> I thought this would be the way to do it:-

> Dim PInfo As PropertyInfo = ty.GetProperty(Nde.Name)
>   If Not PInfo Is Nothing Then
>     Dim t As Type = PInfo.GetType()
>     PInfo.SetValue(ctrl, (CType(Nde.Value, PInfo.GetType())), Nothing)
>   End If

> but CType and DirectCast do not accept PInfo.GetType(). They demand a hand
> coded type e.g. CType(Value, Integer).  Does anybody know of a way to do
> this?

> I look for the TagName via reflection :- Dim PInfo As PropertyInfo =
> ty.GetProperty(Nde.Name) and it returns fine, so it's found the property.
> But how do I then convert the value to the correct Type so I can set the
> property value?

> Demo XML:
> <VISIBLE>True</VISIBLE>
> <Text>Caption :</Text>
> <TextAlign>1</TextAlign>



Mon, 18 Jul 2005 01:11:48 GMT  
 How to CType() dynamically ?
Sorry. I didn't finish before sending. Add the following.

If you are only using primitive types you can use the
System.Convert.ChangeType method but it doesn't cover a huge range of
properties.


Quote:
> This cannot be done using CType. There have been several threads on this
> before. If you want to search via Google you'll get more info on why not.
> The best way to do this is to write your own function that uses a Select
> Case to do the conversion based on the type.



> > I'm working on a form editor a.k.a like in Visual Studio and
sharpdevelop.
> > I save the forms out as xml mainly using to the <code>.ToString
> > </code>methods and then try to convert them back on load.
> > I thought this would be the way to do it:-

> > Dim PInfo As PropertyInfo = ty.GetProperty(Nde.Name)
> >   If Not PInfo Is Nothing Then
> >     Dim t As Type = PInfo.GetType()
> >     PInfo.SetValue(ctrl, (CType(Nde.Value, PInfo.GetType())), Nothing)
> >   End If

> > but CType and DirectCast do not accept PInfo.GetType(). They demand a
hand
> > coded type e.g. CType(Value, Integer).  Does anybody know of a way to do
> > this?

> > I look for the TagName via reflection :- Dim PInfo As PropertyInfo =
> > ty.GetProperty(Nde.Name) and it returns fine, so it's found the
property.
> > But how do I then convert the value to the correct Type so I can set the
> > property value?

> > Demo XML:
> > <VISIBLE>True</VISIBLE>
> > <Text>Caption :</Text>
> > <TextAlign>1</TextAlign>



Mon, 18 Jul 2005 01:20:19 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Crystal Reports: add/delete groups dynamically and starting a new page dynamically

2. cType() Function

3. VB Type Conversion vs. Ctype

4. cType()

5. CType performances with VS2003!!!

6. CInt, CType, or DirectCast?

7. CType function issue in Web service proxy

8. --> Error with CType

9. Dynamic Ctype question

10. CType(Object(), String()) gives me a Object()

11. Casting to a structure with CType gives error

12. Diff between Ctype and CLng?

 

 
Powered by phpBB® Forum Software