472,373 Members | 1,849 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Evaluation Problem

The lat part is not working why ????????

Please help.........

using System;

namespace ConsoleApplication4

{

class reflection

{

public const string mstr = "hellpppo world!";

public System.Windows.Forms.TextBox textBox1;

[STAThread]

static void Main(string[] args)

{

this.textBox1 = new System.Windows.Forms.TextBox();

//

// textBox1

//

this.textBox1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(128)) , ((System.Byte)(64)), ((System.Byte)(0)));

this.textBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

this.textBox1.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(192)) , ((System.Byte)(192)), ((System.Byte)(0)));

this.textBox1.Location = new System.Drawing.Point(24, 48);

this.textBox1.Name = "textBox1";

this.textBox1.TabIndex = 12;

this.textBox1.Text = "textBox1";

this.Controls.Add(this.textBox1);

System.Reflection.FieldInfo fi =

Type.GetType("ConsoleApplication4.reflection") .GetField("mstr");

string temp = (string)fi.GetValue(null);

MessageBox.Show("get string {0}", temp);

/////////// THIS IS NOT WORKING \\\\\\\\\\\

Type.GetType("ConsoleApplication4.reflection") .GetField("textbox1");

string temp = (string)fi.GetValue("Text");

MessageBox.Show("get string {0}", temp);

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\/////////////////////////////////

ERROR Object Type cannot be converted to target type

}

}

}

Nov 15 '05 #1
4 1597
100
1. This program shouldn't work at all.
*Main* is static method and there is no *this*.

2.Beside my first remark if we look at the reflection usage:
System.Reflection.FieldInfo fi =

Type.GetType("ConsoleApplication4.reflection") .GetField("mstr");

string temp = (string)fi.GetValue(null);

MessageBox.Show("get string {0}", temp);

This is ok
/////////// THIS IS NOT WORKING \\\\\\\\\\\
Type.GetType("ConsoleApplication4.reflection") .GetField("textbox1");

You have forgotten *fi = ....*. You are using FieldInfo for the *mstr* field.
BTW this should return *null* since IFAIK the default binding is case sensitive. You shoud use "textBox1";
string temp = (string)fi.GetValue("Text");

You are trying ot get a value for *mstr* field of object of type String. String doesn't have this field. GetValue exepects object which field value will be returned. The type of this object has to be type that declares or inhertis the field. String doesn't declares the field nor inherits from reflection class. This is the exception that you've got.

The correct code should look something like the following:
reflection r = new reflection();

FiledInfo fi = Type.GetType("ConsoleApplication4.reflection") ..GetField("textBox1");

//Alternatives for obtaining type object :

//typeof(ConsoleApplication4.reflection)

//r.GetType();

TextBox tb = (TextBox))fi.GetValue(r);

MessageBox.Show("get string {0}", tb.Text);

HTH
B\rgds
100
Nov 15 '05 #2
Hi again Nicolas,

Type.GetField( ) expect the field name, the field name is "Text" , "textBox1" is the variable containing the instance of TextBox.
therefore the correct construction is:
FieldInfo fi = Type.GetType("ConsoleApplication4.reflection") ..GetField("textBox1") ;

This line return a FieldInfo instance, now to get the value of that instance you could use:
object o = fi.GetValue( this);

now you have the value of the field ( textBox1 ) for a particular instance ( this )

to get the value of the property "Text" of it you have to do something similar:
PropertyInfo pi = o.GetType() .GetProperty("Text") ;
And finally evaluate it:

string text = pi.GetValue( textBox1, null).ToString();
Hope you understand it, otherwise just post back ;)

In short you could do this:
string text = Type.GetType("ConsoleApplication4.reflection") ..GetField("textBox1").GetType() .GetProperty("Text").GetValue( textBox1, null).ToString();

Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

message news:OP**************@TK2MSFTNGP12.phx.gbl...
The lat part is not working why ????????

Please help.........

using System;

namespace ConsoleApplication4

{

class reflection

{

public const string mstr = "hellpppo world!";

public System.Windows.Forms.TextBox textBox1;

[STAThread]

static void Main(string[] args)

{

this.textBox1 = new System.Windows.Forms.TextBox();

//

// textBox1

//

this.textBox1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(128)) , ((System.Byte)(64)), ((System.Byte)(0)));

this.textBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

this.textBox1.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(192)) , ((System.Byte)(192)), ((System.Byte)(0)));

this.textBox1.Location = new System.Drawing.Point(24, 48);

this.textBox1.Name = "textBox1";

this.textBox1.TabIndex = 12;

this.textBox1.Text = "textBox1";

this.Controls.Add(this.textBox1);

System.Reflection.FieldInfo fi =

Type.GetType("ConsoleApplication4.reflection") .GetField("mstr");

string temp = (string)fi.GetValue(null);

MessageBox.Show("get string {0}", temp);

/////////// THIS IS NOT WORKING \\\\\\\\\\\

Type.GetType("ConsoleApplication4.reflection") .GetField("textbox1");

string temp = (string)fi.GetValue("Text");

MessageBox.Show("get string {0}", temp);

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\/////////////////////////////////

ERROR Object Type cannot be converted to target type

}

}

}

Nov 15 '05 #3
Hi,
I just noted that you are doing all this inside the main() method of a console application , this introduce a few others things:

1- the main method is declared as static, therefore there is no instance associated, you cannot access textBox1 as it's not marked as static.
2- If you want to create a windows application to this this, you need to include the System.Windows.Forms namespace and inside the main method make this call:

Application.Run(new Form1());

This create a new instance of the form you need and create the message loop to receive events.

The code you are written you could write it in the Load event handler of the form.

Sorry for not reading well your original post, long time without caffeine I think :)

but the explanatio of how to get the field and later evaluate a property of it is still correct.

Cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote in message news:Oh****************@TK2MSFTNGP10.phx.gbl...
Hi again Nicolas,

Type.GetField( ) expect the field name, the field name is "Text" , "textBox1" is the variable containing the instance of TextBox.
therefore the correct construction is:
FieldInfo fi = Type.GetType("ConsoleApplication4.reflection") ..GetField("textBox1") ;

This line return a FieldInfo instance, now to get the value of that instance you could use:
object o = fi.GetValue( this);

now you have the value of the field ( textBox1 ) for a particular instance ( this )

to get the value of the property "Text" of it you have to do something similar:
PropertyInfo pi = o.GetType() .GetProperty("Text") ;
And finally evaluate it:

string text = pi.GetValue( textBox1, null).ToString();
Hope you understand it, otherwise just post back ;)

In short you could do this:
string text = Type.GetType("ConsoleApplication4.reflection") ..GetField("textBox1").GetType() .GetProperty("Text").GetValue( textBox1, null).ToString();

Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

message news:OP**************@TK2MSFTNGP12.phx.gbl...
The lat part is not working why ????????

Please help.........

using System;

namespace ConsoleApplication4

{

class reflection

{

public const string mstr = "hellpppo world!";

public System.Windows.Forms.TextBox textBox1;

[STAThread]

static void Main(string[] args)

{

this.textBox1 = new System.Windows.Forms.TextBox();

//

// textBox1

//

this.textBox1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(128)) , ((System.Byte)(64)), ((System.Byte)(0)));

this.textBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

this.textBox1.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(192)) , ((System.Byte)(192)), ((System.Byte)(0)));

this.textBox1.Location = new System.Drawing.Point(24, 48);

this.textBox1.Name = "textBox1";

this.textBox1.TabIndex = 12;

this.textBox1.Text = "textBox1";

this.Controls.Add(this.textBox1);

System.Reflection.FieldInfo fi =

Type.GetType("ConsoleApplication4.reflection") .GetField("mstr");

string temp = (string)fi.GetValue(null);

MessageBox.Show("get string {0}", temp);

/////////// THIS IS NOT WORKING \\\\\\\\\\\

Type.GetType("ConsoleApplication4.reflection") ..GetField("textbox1");

string temp = (string)fi.GetValue("Text");

MessageBox.Show("get string {0}", temp);

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\/////////////////////////////////

ERROR Object Type cannot be converted to target type

}

}

}

Nov 15 '05 #4
Thank you but I still have some problem as I explain here.
1 application with form1
1 dll which is holding some usercontrol etc...
The idea is to use the user control dll to alter the Form1 controls
When Form1 load then I call and initialize my usercontrol dll
First I notice that the controls have too be public and not private >>> is that correct?
Can this work with private or do I have to change all the default setting of every control to public?
Second it look like I dont really have an handle of the source class (Form1 from the usercontrol dll) >>>>

When this is run within the form1_Load all together it works fine, but not when the code reside into the dll.
It must be something very stupid that I'm doing here. But I'm loosing my hair way too fast on that one.
Thank for not letting going hairless.

I marked the code where it stop working.

FORM1
private void Form1_Load(object sender, System.EventArgs e)

{
// The control Button1 is well set in the Form1 has it is used correctly.

inTouch.Tools.Property.GetControlProperties getProp = new inTouch.Tools.Property.GetControlProperties(this, this.Button1,"BackColor","ffffff80");

}

USERCONTROL DLL
namespace inTouch.Tools.Property

{

public class GetControlProperties

{

public GetControlProperties(Form myForm, Control myControl , string myProperty, object myPropValue)

{

// I assume here that this is BackColor

// then I receive a color for myPropValue

System.Reflection.FieldInfo fi = Type.GetType(myForm.GetType().ToString()).GetField (myControl.ToString()) ;
> STOP HERE WITH THIS ERROR (Description: Object reference not set to an instance of an object.)


object o = fi.GetValue(myControl);

<<<<<<

System.Reflection.PropertyInfo pi = o.GetType().GetProperty(myProperty);

string text = pi.GetValue(myControl, null).ToString();

MessageBox.Show(text.ToString());

// NOW Set the new Property

o.GetType().GetProperty(myProperty).SetValue(o,Col or.FromArgb((int)myPropValue), null);

}

}

}



"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote in message news:Oh****************@TK2MSFTNGP10.phx.gbl...
Hi again Nicolas,

Type.GetField( ) expect the field name, the field name is "Text" , "textBox1" is the variable containing the instance of TextBox.
therefore the correct construction is:
FieldInfo fi = Type.GetType("ConsoleApplication4.reflection") ..GetField("textBox1") ;

This line return a FieldInfo instance, now to get the value of that instance you could use:
object o = fi.GetValue( this);

now you have the value of the field ( textBox1 ) for a particular instance ( this )

to get the value of the property "Text" of it you have to do something similar:
PropertyInfo pi = o.GetType() .GetProperty("Text") ;
And finally evaluate it:

string text = pi.GetValue( textBox1, null).ToString();
Hope you understand it, otherwise just post back ;)

In short you could do this:
string text = Type.GetType("ConsoleApplication4.reflection") ..GetField("textBox1").GetType() .GetProperty("Text").GetValue( textBox1, null).ToString();

Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

message news:OP**************@TK2MSFTNGP12.phx.gbl...
The lat part is not working why ????????

Please help.........

using System;

namespace ConsoleApplication4

{

class reflection

{

public const string mstr = "hellpppo world!";

public System.Windows.Forms.TextBox textBox1;

[STAThread]

static void Main(string[] args)

{

this.textBox1 = new System.Windows.Forms.TextBox();

//

// textBox1

//

this.textBox1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(128)) , ((System.Byte)(64)), ((System.Byte)(0)));

this.textBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

this.textBox1.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(192)) , ((System.Byte)(192)), ((System.Byte)(0)));

this.textBox1.Location = new System.Drawing.Point(24, 48);

this.textBox1.Name = "textBox1";

this.textBox1.TabIndex = 12;

this.textBox1.Text = "textBox1";

this.Controls.Add(this.textBox1);

System.Reflection.FieldInfo fi =

Type.GetType("ConsoleApplication4.reflection") .GetField("mstr");

string temp = (string)fi.GetValue(null);

MessageBox.Show("get string {0}", temp);

/////////// THIS IS NOT WORKING \\\\\\\\\\\

Type.GetType("ConsoleApplication4.reflection") ..GetField("textbox1");

string temp = (string)fi.GetValue("Text");

MessageBox.Show("get string {0}", temp);

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\/////////////////////////////////

ERROR Object Type cannot be converted to target type

}

}

}

Nov 15 '05 #5

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

Similar topics

22
by: Daniel Déchelotte | last post by:
Hi, Once I was happy with my new deezign, I noticed how bad it breaks in IE (all versions I tried). Since IE7 didn't give acceptable results, I wrote a simplified style that I would fetch to IE....
16
by: Bhushit Joshipura | last post by:
This post contains one question and one proposal. A. May I know why order of evaluation of arguments is not specified in C/C++? I asked a question in comp.lang.c++ for the following...
3
by: marco_segurini | last post by:
Hi, when the following code is executed bool Test1(); bool Test2(); .... if (Test1() || Test2()) { ... } ....
15
by: Jens.Toerring | last post by:
Hi, I have a possibly rather stupid question about the order of evaluation in a statement like this: result = foo( x ) - bar( y ); How can I make 100% sure that foo(x) is evaluated before...
4
by: Frank Wallingford | last post by:
Note: For those with instant reactions, this is NOT the common "why is i = i++ not defined?" question. Please read on. I came across an interesting question when talking with my colleagues....
77
by: berns | last post by:
Hi All, A coworker and I have been debating the 'correct' expectation of evaluation for the phrase a = b = c. Two different versions of GCC ended up compiling this as b = c; a = b and the other...
9
by: John Smith | last post by:
I've been playing with splint, which returns the following warning for the code below: statlib.c: (in function log_norm_pdf) statlib.c(1054,31): Expression has undefined behavior (left operand...
9
by: sturlamolden | last post by:
Python allows the binding behaviour to be defined for descriptors, using the __set__ and __get__ methods. I think it would be a major advantage if this could be generalized to any object, by...
54
by: Rasjid | last post by:
Hello, I have just joined and this is my first post. I have never been able to resolve the issue of order of evaluation in C/C++ and the related issue of precedence of operators, use of...
39
by: Boltar | last post by:
Why does C do lazy evaluation for logical boolean operations but not bitwise ones? Ie: the following program prints "1 2" , not "1 1" under gcc main() { int a = 1; int b = 1; 0 && ++a;
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

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.