How to convert a string variable dynamic to another type?

A

a.frank81

I have problems with this Code:

Public Sub SetPropertyValue(ByVal strPropertyPath As String, ByVal obj
As Object, ByVal Value As Object)
.....
Dim t_PropertyInfos() As System.Reflection.PropertyInfo =
(t_obj.GetType).GetProperties
For ii = 0 To t_PropertyInfos.Length - 1
If t_PropertyInfos(ii).Name = t_strProperty Then
t_PropertyInfos(ii).SetValue(t_obj, Value, Nothing)
End If
Next

I get the following error message:
An unhandled exception of type 'System.ArgumentException' occurred in
mscorlib.dll
Additional information: Zieltyp kann nicht zu primitiven Typ erweitert
werden.

"t_PropertyInfos(ii)" required a Double und the "Value" is a String,
for example "10".

I can get the Type of my Property with:
Dim t_type As Type = t_PropertyInfos(ii).PropertyType

But how can i convert my Value in this type?

I tryed:
Value = CType(Value, t_type)
and alsoe
Value = DirectCast(Value, t_type)

There are both a Syntax Error:
"Type 't_type' is not defined.

Is there any method to convert a variable to a dynamicl type?
 
G

Guest

I'm not sure, but have you tried the following?

CType(Value, t_obj.GetType)
or maybe
CType(Value, GetType(t_obj))

Hope this helps,

Joris
 
A

Andreas

This would be go when "t_PropertyInfos(ii)" required allways a Double,
but in the "t_PropertyInfos(ii+1) requires perhaps a Int. So I don't
know at Design-Time which type my property need.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top