473,569 Members | 2,735 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Assign value to an object created by a reference

I want to setup properties value to an object created by reference.

I do the following.

T t = Activator.Creat eInstance<T>();
Type newItem = typeof(T);
newItem.GetProp erty("PropertyA ").SetValue (t, "Hello", null);

It works fine, but I see that there is a GetProperties method that returns
all properties available for a certain
type, and I suppose that instead calling a getproperty every time like the
code above, modifying the value to the
array of PropertyInfos it should be faster and with less memory consuming.

Unfortunately when I try to do something like that

PropertyInfors[] pi = newItem.GetProp erties(MyBindin gValues);
pi.SetValue("He llo", 0);

the compiler at run-time say that I cannot do something like that and throw
an InvalidArgument CastException.

I was looking for some sample over the net, but nothing usefull. Someone
can let me understand?

Thank you
Andrea

Feb 22 '06 #1
6 2090
"andrea" <go**********@f reemail.it> a écrit dans le message de news:
9f************* *************@n ews.microsoft.c om...

| T t = Activator.Creat eInstance<T>();
| Type newItem = typeof(T);
| newItem.GetProp erty("PropertyA ").SetValue (t, "Hello", null);
Hi Andrea, I hadn't forgotten you, I was trying to get the time to create an
example and am about half done.

You are on the right track here and, yes, you do need to use
GetProperties() .

| Unfortunately when I try to do something like that
|
| PropertyInfors[] pi = newItem.GetProp erties(MyBindin gValues);
| pi.SetValue("He llo", 0);

Shoould be something like this :

PropertyInfo[] propInfos = typeof(T).GetPr operties();
foreach (PropertyInfo pi in propInfos)
{
pi.SetValue("He llo", null);
...

....except you would have to get the property names from a list and then get
the data from a dataset.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Feb 22 '06 #2
J>Hi Andrea, I hadn't forgotten you,
Don't worry, meanwhile I used your suggestion and finally I got a working
code.

J> PropertyInfo[] propInfos = typeof(T).GetPr operties();
J> foreach (PropertyInfo pi in propInfos)
J> {
J> pi.SetValue("He llo", null);
J> ...

About your sample, this mean that use the recursion while getting data from
db that
should be necessary casted to the right type isn't the suitable solution.
Is it right?

I suppose that make a lot call as I do or do it in a cycle, internally do
the same things?
Right?

Bye
Andrea

Feb 22 '06 #3
Joanna ... It doesn't work. I continue get A TargetException ... and I don't
know.
I use the same variable used for the extended item.GetPropert ies().SetValue
....

Thanks
Andrea


Feb 22 '06 #4
"andrea" <go**********@f reemail.it> a écrit dans le message de news:
9f************* *************@n ews.microsoft.c om...

| Joanna ... It doesn't work. I continue get A TargetException ... and I
don't
| know.
| I use the same variable used for the extended
item.GetPropert ies().SetValue

I'm sorry, but you will have to explain better what the problem is. Please
post the code and point out *exactly* the point that is causing the error.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Feb 22 '06 #5
The problem is in the call to pi.SetValue(). The PropertyInfo object is
pure reflective data; in other words, it knows only about a type, and
nothing about any particular instance of that type. Look at how you got
it:

PropertyInfo[] propinfos = typeof(T).GetPr operties();

So, when you call SetValue, you have to say for which object (of type
T) you want to set the property value. In other words, your call has to
look something more like this:

pi.SetValue(t, "Hello", null);

where t is the original object created in andrea's example:

T t = Activator.Creat eInstance<T>();

Feb 23 '06 #6
BW> PropertyInfo[] propinfos = typeof(T).GetPr operties();

that's the point. I was passsing the type itself and not the instance object.
Sorry for the mistake and thanks for the reply

Bye
Andrea

Feb 23 '06 #7

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

Similar topics

4
12552
by: Eric | last post by:
How can I dynamically assign an event to an element? I have tried : (myelement is a text input) document.getElementById('myelement').onKeyUp = "myfnc(param1,param2,param3)"; document.getElementById('myelement') = new Function("myfnc(param1,param2,param3)");
9
2921
by: ckerns | last post by:
I want to loop thru an array of controls,(39 of them...defaults = 0). If value is null or non-numeric I want to assign the value of "0". rowString = "L411" //conrol name if (isNaN(eval ("document.forms."+rowString+".value")) == true ) { //this alert works if the value is a letter,i.e,"a" alert("You have entered an non-numeric...
16
25399
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have the properties: mode1, mode2, and mode3. This seems simple but I can't quite figure it out... Any ideas anyone?
1
18149
by: Marc | last post by:
Hi! I'm working with a C# client that calls a php web service. I've created a wrapper to call the service using .NET wsdl tool (adding a web reference). The call to the server works fine, it is serialized correctly, and the server returns a response (I've captured the response and it's correct!) but when the .NET deserialize this...
7
2507
by: Edward Diener | last post by:
Since implement the assign operator for reference types eliminates the ability to assign a reference object to a reference variable of the same type or base class of that type, I assume that implementing the assign operator ( = ), to assign the value of a type from one object to another rather than the reference, should only be done for value...
42
1917
by: blisspikle | last post by:
I tried closely copying some code that I found on this group for assigning a type at runtime, but I cannot get it to work. Can someone see what is wrong with my logic? Thanks, Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim s As String s = "blah"
18
3922
by: joaotsetsemoita | last post by:
Hello everyone, I'm having troubles assigning an onclick event to a cell. Im trying something like cursorPoint.cells.style.cursor = "hand"; cursorPoint.cells.width = "20"; cursorPoint.cells.onclick = "alert('this is a test');" cursorPoint.cells.alt = "Select the columns"; cursorPoint.cells.innerHTML = "&nbsp;"
6
2618
by: TriFuFoos | last post by:
Hi there, I was wondering if anyone knew if/how to assign an event to a global variable? I tried to do it and IE 7 came back with an error saying "Member not found" My code looked similar to the following: var globalEvevnt; function showPopup(event){ globalEvent = event;
5
8071
by: howa | last post by:
Hi, Consider a simple example, e.g. var a = { 'a': 'b', 'c': 'd' }
0
7609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7921
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. ...
0
8118
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7666
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7964
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...
0
6278
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5504
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...
0
5217
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...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.