473,624 Members | 1,957 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 SetObjectProper ty(ByRef obj As Object, _
ByValPropertyNa me As String, _
ByVal PropertyValue As String)
Dim wType As System.Type = obj.GetType
Dim wProp As Reflection.Prop ertyInfo = _
wType.GetProper ty(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 3102


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 AssemblyPropert ies

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

'''''''' Dim indivReportProp erty As PropertyInfo =
newObject.GetTy pe().GetPropert y(prop.Name)

'''''''' If Not (indivReportPro perty 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"

'''''''' indivReportProp erty.SetValue(n ewObject, prop.GetValue(n ewObject,
Nothing), Nothing)

'''''''' End If

'''''''' End If

''''''''Next prop



"Matt Weaver" <mw********@yah oo.com> wrote in message
news:ec******** ******@TK2MSFTN GP09.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 SetObjectProper ty(ByRef obj As Object, _
ByValPropertyNa me As String, _
ByVal PropertyValue As String)
Dim wType As System.Type = obj.GetType
Dim wProp As Reflection.Prop ertyInfo = _
wType.GetProper ty(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.ChangeT ype(obj, type);

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

//pass the values using an interface:
((ISomeInterfac e)obj).Paramete rs = keyValueCollect ion;
//use the values:
if (Parameters["count"] != null)
{
_count = Int32.Parse(Par ameters["count"]);
}
else
{
throw new Exception("coun t parameter expected");
}
Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"Matt Weaver" <mw********@yah oo.com> wrote in message
news:ec******** ******@TK2MSFTN GP09.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 SetObjectProper ty(ByRef obj As Object, _
ByValPropertyNa me As String, _
ByVal PropertyValue As String)
Dim wType As System.Type = obj.GetType
Dim wProp As Reflection.Prop ertyInfo = _
wType.GetProper ty(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.ChangeT ype 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.ChangeT ype(obj, type);

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

//pass the values using an interface:
((ISomeInterfac e)obj).Paramete rs = keyValueCollect ion;
//use the values:
if (Parameters["count"] != null)
{
_count = Int32.Parse(Par ameters["count"]);
}
else
{
throw new Exception("coun t 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
2160
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 registration to business office. We are considering useing Java or Python. I for one don't like Java because I feel the GUI is clunky. I also think that we could produce quality programs faster in Python.
5
8008
by: Radde | last post by:
HI, Are ther any pitfalls for dynamic cast in type safe downcasting..
2
2560
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 for all business entities) dictates that the implementing class expose a Dirty property (for state). The base class (that actually manages state, once for all inheritors)
4
4953
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 correctParams = new object for(int i... Type t = Type.GetType(tiB.GetTypeName()) DynamicProxy proxy = new DynamicProxy(t, orderedParams, tiA) correctParams = /*TODO: CAST into type t*/ proxy.GetTransparentProxy()
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 very basic scenario: using System;
6
6392
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. Of course it first tries to obtain the appropriate TypeConverter. However, for some types there are no applicable type converters. Consider a custom class "Dummy" from a linked assembly (in my scenario, neither the Dummy type nor its assembly...
4
2198
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 that several classes inherit from. From time to time I need to add new inherited types so I want something that is as versatile as possible, preferably not requiring naming all subtypes in a switch statement.
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 a pointer from a base class to a derived class. For example: class Base{ public: Base() {} virtual ~Base() {} void func1(int);
6
4086
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 do it. They both inherit from parent, and I tried holding a parent variable, and then casting it down to either of the children, but I can't see either of the childrens unique methods. Thanks in advance for the help.
28
6037
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 C-professional, just a hobby-programmer trying to teach it myself. I found C rather difficult without those lists (and corresponding string-functions). Slowly getting into C-strings, I thought, what if I take a C-string and
0
8238
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8680
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8478
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6111
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5565
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2607
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1786
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1485
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.