473,399 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Reflection, objects, and comparing types

Hi, I'm a vb.net developer getting the hang of c# and I've gotten stuck on trying to compare types. In the following block, I'm populating an object from a DataTable. The table's data types aren't always the right type so as I loop through I want to compare types and optionally try to convert to make the data match the object property types.

Expand|Select|Wrap|Line Numbers
  1.             Type t = _Item.GetType();
  2.  
  3.             foreach (DataRow r in dtIT.Rows)
  4.                 {
  5.                     foreach (PropertyInfo p in t.GetProperties())
  6.                     { 
  7.                         //assign any matching column names to object properties
  8.                         if (dtIT.Columns[p.Name] != null)
  9.                         {
  10.                            if (!(r[p.Name].GetType is p.GetType))
  11.                             {
  12.                                 //convert type if possible
  13.                             }
  14.  
  15.                            p.SetValue(_Item, r[p.Name], null);
  16.                         }
  17.                     }
  18.  
what happens in the above block is I can't even build and Intellisense is flagging the p in p.GetType with this message:

Error 1 The type or namespace name 'p' could not be found (are you missing a using directive or an assembly reference?) ...\Classes\ItemTreeData.cs 61 55 DAL

Can anyone tell me what I'm doing wrong or have any suggestions for me? Thanks!
May 20 '10 #1

✓ answered by tlhintoq

error: cannot be applied to operands of type 'method group' and 'method group'
Means you are calling a method without the parenthises for the paramaters. Sorry I should have seen it earlier but I've been up all night.

.GetType is a method and requires .GetType()

Expand|Select|Wrap|Line Numbers
  1. if (r[p.Name].GetType() == p.GetType())

5 3109
tlhintoq
3,525 Expert 2GB
try it without the "is"... I *think* 'is' is only VB. Instead use the 'is equal to' double equal sign

Expand|Select|Wrap|Line Numbers
  1. if (!(r[p.Name].GetType == p.GetType))
May 20 '10 #2
jkmyoung
2,057 Expert 2GB
typeof(p)
?
May 20 '10 #3
Wow, you guys are quick! Thanks for the replies.

Expand|Select|Wrap|Line Numbers
  1. if (r[p.Name].GetType == p.GetType)
Results in the following error in the IDE (won't build):
Error 1 Operator '==' cannot be applied to operands of type 'method group' and 'method group' C:\Users\mburt\Documents\Visual Studio 2010\Projects\eCommerce\EComm_DAL\Classes\ItemTree Data.cs 61 33 DAL

Expand|Select|Wrap|Line Numbers
  1.  if (r[p.Name].GetType == typeof(p))
In the above the p gets the red underline from Intellisense with the following error:
Error 1 The type or namespace name 'p' could not be found (are you missing a using directive or an assembly reference?) C:\Users\mburt\Documents\Visual Studio 2010\Projects\eCommerce\EComm_DAL\Classes\ItemTree Data.cs 61 61 DAL

Which seems odd to me because I use p.name without incident a few lines above.

Thanks again!
Matt
May 20 '10 #4
tlhintoq
3,525 Expert 2GB
error: cannot be applied to operands of type 'method group' and 'method group'
Means you are calling a method without the parenthises for the paramaters. Sorry I should have seen it earlier but I've been up all night.

.GetType is a method and requires .GetType()

Expand|Select|Wrap|Line Numbers
  1. if (r[p.Name].GetType() == p.GetType())
May 20 '10 #5
Looks like that was it! Thank you SO MUCH!
May 20 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Chris Blanco | last post by:
I am building a Dynamic assembly loader which will scan and monitor a directory, when a new assembly gets placed in that directory it will examine the directory to see if there is a class that is...
3
by: Cliff Harris | last post by:
I have an odd question that I'm hoping someone can help with. I simply (or not simply?) need to enumerate through all of the data types in the .Net framework. I do understand that if this is...
3
by: Simon Harvey | last post by:
Hi all, I hope someone can help me with the following: I have a number of usercontrols that I've made which provides certain audit functions for any data inserted into it. Each audit control...
0
by: rein.petersen | last post by:
Hi All, I've been working on a means to create dynamic asemblies (modules actually) from parsing XSD schemas which creates a perfect .net representation of the schema at hand (dynamically). ...
3
by: tinman | last post by:
Hi.... Assume Function A in an application calls Function GetSomeData in another assembly..... which then is the prefered method to return the SqlDatareader object back to Function A (and why...
2
by: Dennis | last post by:
Why won't either one of the below If expressiions evaluate to TRUE: Dim ob As Object ob = "This String" If ob.GetType Is Type.GetType("String") Then ..... End If If ob.GetType is...
2
by: Ronen Tidhar | last post by:
I would like your help to find out how do I retrieve by reflection all the types in web application? In VS 2003 I used to do: public class Global : System.Web.HttpApplication { protected...
0
by: IR | last post by:
Hi, I'm trying to store values of an enum type in a std::bitset container, in a contiguous manner. eg. typedef enum { meValue1 = 0xbaad, meValue2 = 0xbeef,
3
by: Mike J | last post by:
Hi..need help comparing types example method istypeof(object someobj) { if (someobj=int32) } ya kinda get my idea here.... MJ
6
by: Cralis | last post by:
Hi guys, Someone once said, 'You can do that with reflection'. I can't recall what it was I was trying to do at the time, but then he said, 'Any developer knows what reflection is...'. I kept...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.