473,500 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic casting

I'm trying to setup a sub that would assign the properties of a
webcontrol based on data in a table. I've setup a routine which looks
like this:

Private Sub SetObjectProperty(ByRef obj As Object, _
ByValPropertyName As String, _
ByVal PropertyValue As String)
Dim wType As System.Type = obj.GetType
Dim wProp As Reflection.PropertyInfo = _
wType.GetProperty(PropertyName)
wProp.SetValue(obj, PropertyValue, Nothing)
End Sub

It works well when the property is a string, but when it's boolean I get
this error: "Object of type 'System.String' cannot be converted to
type 'System.Boolean'"

I can get the type of the property (in this case, System.Boolean), I
have the value I'd get if I used .ToString(). .NET has to have some
function that'd dynamically cast the string back to Boolean (or whatever
data type), right? I'm trying to avoid a giant if-then structure (if
type = system.boolean then cast as boolean).

TIA!
-Matt

Mar 28 '06 #1
3 3090


I think you wrote it correct, but because of the way the eval works...
you'll have to hack in some case statements.

Here is my code... notice I check the "CanWrite". That probably isn't your
issue, but I list it there anyways.

I think you'll need to hack in some case statements on the type
unforunately.
'''''''''the next loop is basically a cloning mechanism.

''''''''Dim prop As PropertyInfo

''''''''For Each prop In AssemblyProperties

'''''''' If prop.CanWrite Then ' only update properties which can be written
to

'''''''' Dim indivReportProperty As PropertyInfo =
newObject.GetType().GetProperty(prop.Name)

'''''''' If Not (indivReportProperty Is Nothing) Then 'handle the case where
the objects are not perfect copies of one another

'''''''' 'this sets the "new object" property to the getvalue of the "old
object"

'''''''' indivReportProperty.SetValue(newObject, prop.GetValue(newObject,
Nothing), Nothing)

'''''''' End If

'''''''' End If

''''''''Next prop



"Matt Weaver" <mw********@yahoo.com> wrote in message
news:ec**************@TK2MSFTNGP09.phx.gbl...
I'm trying to setup a sub that would assign the properties of a
webcontrol based on data in a table. I've setup a routine which looks
like this:

Private Sub SetObjectProperty(ByRef obj As Object, _
ByValPropertyName As String, _
ByVal PropertyValue As String)
Dim wType As System.Type = obj.GetType
Dim wProp As Reflection.PropertyInfo = _
wType.GetProperty(PropertyName)
wProp.SetValue(obj, PropertyValue, Nothing)
End Sub

It works well when the property is a string, but when it's boolean I get
this error: "Object of type 'System.String' cannot be converted to
type 'System.Boolean'"

I can get the type of the property (in this case, System.Boolean), I
have the value I'd get if I used .ToString(). .NET has to have some
function that'd dynamically cast the string back to Boolean (or whatever
data type), right? I'm trying to avoid a giant if-then structure (if
type = system.boolean then cast as boolean).

TIA!
-Matt

Mar 28 '06 #2
Well, you can use Convert.ChangeType(obj, type);

For situations like this, I often find it better to ass in a
NameValueCollection collection into the object, and let it figure out how to
do thing:

//pass the values using an interface:
((ISomeInterface)obj).Parameters = keyValueCollection;
//use the values:
if (Parameters["count"] != null)
{
_count = Int32.Parse(Parameters["count"]);
}
else
{
throw new Exception("count parameter expected");
}
Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"Matt Weaver" <mw********@yahoo.com> wrote in message
news:ec**************@TK2MSFTNGP09.phx.gbl...
I'm trying to setup a sub that would assign the properties of a webcontrol
based on data in a table. I've setup a routine which looks like this:

Private Sub SetObjectProperty(ByRef obj As Object, _
ByValPropertyName As String, _
ByVal PropertyValue As String)
Dim wType As System.Type = obj.GetType
Dim wProp As Reflection.PropertyInfo = _
wType.GetProperty(PropertyName)
wProp.SetValue(obj, PropertyValue, Nothing)
End Sub

It works well when the property is a string, but when it's boolean I get
this error: "Object of type 'System.String' cannot be converted to type
'System.Boolean'"

I can get the type of the property (in this case, System.Boolean), I have
the value I'd get if I used .ToString(). .NET has to have some function
that'd dynamically cast the string back to Boolean (or whatever data
type), right? I'm trying to avoid a giant if-then structure (if type =
system.boolean then cast as boolean).

TIA!
-Matt

Mar 28 '06 #3
Convert.ChangeType worked for me. I'll keep the rest of your message in
mind if it breaks with more complicated object types.

Karl Seguin [MVP] wrote:
Well, you can use Convert.ChangeType(obj, type);

For situations like this, I often find it better to ass in a
NameValueCollection collection into the object, and let it figure out how to
do thing:

//pass the values using an interface:
((ISomeInterface)obj).Parameters = keyValueCollection;
//use the values:
if (Parameters["count"] != null)
{
_count = Int32.Parse(Parameters["count"]);
}
else
{
throw new Exception("count parameter expected");
}
Karl


Mar 28 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
2151
by: Jason Tesser | last post by:
I work for at a college where I am one of 2 full-time developers and we are looking to program a new software package fro the campus. This is a huge project as it will include everything from...
5
7992
by: Radde | last post by:
HI, Are ther any pitfalls for dynamic cast in type safe downcasting..
2
2549
by: Zac | last post by:
Alright anyone who has 2c throw it in... I am working through a custom xml serializer and have come upon a conundrum, given our class design. The interface implemented on the base class (base...
4
4936
by: Val | last post by:
Hi I am using a DynamicProxy (class that inherits from RealProxy). I don't know the type of my Transparant Proxy at compile time. Is there a possibility to have a dynamic casting object...
2
673
by: Martin Hart - Memory Soft, S.L. | last post by:
Hi all: I still very new to the .NET world and don't know if what I am asking is due to an over-imaginative imagination or the fact that I have read too many fiction books!! Let me show you a...
6
6372
by: Philipp Schumann | last post by:
Hi, I have a need for "dynamic type casting": in other words, in a "MyConvert" method I get passed an Object "value" and a Type "type" and the method should attempt to convert value into type. ...
4
2181
by: Zark3 | last post by:
Hi all, I was wondering if anybody could enlighten me on the possibility of dynamic casting. Or, well, whether or not I'm actually trying to do this the right way. What I have is a base class...
2
461
by: Mike Stevenson | last post by:
Hi. I'm in the process of re-learning all the C++ I forgot from college, and I'm starting to get into some virgin (or at least only a couple times) territory. I have some questions about casting...
6
4080
by: DaTurk | last post by:
Hi, I have three interfaces lets call them parent, child-A, and child-B. I want to have one variable, that can be either child-A, or child-B at run time. I just can't seem to figure out how to...
28
6008
by: hlubenow | last post by:
Hello, I really like Perl and Python for their flexible lists like @a (Perl) and a (Python), where you can easily store lots of strings or even a whole text-file. Now I'm not a...
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
6905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7395
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5485
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4921
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3108
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.