470,811 Members | 1,282 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,811 developers. It's quick & easy.

How to convert a string variable dynamic to another type?

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?

Jun 23 '06 #1
4 2736
Dim MyDouble As Double = Double.Parse(MyString);
<a.*******@gmx.ch> wrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
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?

Jun 23 '06 #2
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.*******@gmx.ch" wrote:
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?

Jun 23 '06 #3
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.
Stuart Irving schrieb:
Dim MyDouble As Double = Double.Parse(MyString);


Jun 26 '06 #4
There are also both a Syntax Error

Joris Zwaenepoel schrieb:
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


Jun 26 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

134 posts views Thread by James A. Donald | last post: by
7 posts views Thread by Jim Bancroft | last post: by
2 posts views Thread by Martin | last post: by
5 posts views Thread by Allerdyce.John | last post: by
7 posts views Thread by Saber | last post: by
2 posts views Thread by yinglcs | last post: by
3 posts views Thread by mrajanikrishna | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.