Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

really need help with a simple thing

OK, I know I have been at this too long. Here is what I am trying to do.  I want to diplay a string in a text box. Sounds simple enough

TextBox1.Text = myPropInfo.Name.ToString & " - " & em2.MyTicket.FooBar

Touble is that FooBar needs to be replaced by the actual property i am interested in at runtime.

Coutesey of reflection I know what the valid public properties of the class are - and that they are all strings.

Trouble is when I try and build this out like so -

TextBox1.Text = myPropInfo.Name.ToString & " - " & "m2.MyTicket." & theProperty

all I get back in TextBox1 is

FirstName - m2.MyTicket.theProperty

That is the theProperty is never evaluated and given it's real value i.e. if hardcoded m2.MyTicket.FirstName I would get back

FirstName - Viktor

Anythought would be appeciated - if you need more info, just let me know.

Thanks a million from me and my brain 

My Brain Hurts
Sunday, October 06, 2002

Are you looking for something like this?

using System;
using System.Reflection;

namespace ReflectionTest
{
    class Test
    {
        [STAThread]
        static void Main(string[] args)
        {
            A AObject = new A();
            TypeDelegator td = new TypeDelegator(AObject.GetType());
            string PropName = Console.ReadLine();
            while (PropName != "end"){
                try{
                    PropertyInfo pi = td.GetProperty(PropName);
                    Console.WriteLine("Property '{0}' has value '{1}'", PropName, pi.GetValue(AObject, null));
                }
                catch{
                    Console.WriteLine("Errors occured while trying to get value of the '{0}' property", PropName);
                }
                PropName = Console.ReadLine();
            }
        }
    }

    public class A{
        private int _a = 1;
        private string _b = "Hello!";
        private bool _c = true;

        public A(){
            _c = false;
        }

        public int a{
            get{return _a;
            }
        }

        public string b{
            get{return _b;
            }
        }

        public bool c{
            get{return _c;
            }
        }
    }
}

Mikhail Andronov
Monday, October 07, 2002

I am not sure if what you mention is what I need. Reflection is not the issue - I should have never mentioned it.  Let me simplify:

This is what I have...

public Class foo
private m_Name as string
private m_Job as string
Public Property Name
Public Property Job
Sub New()
m_Name = "George"
m_Job="Union Boss"
End Sub
End Class

Public function GetThePropertyValueIWant (byVal TheProperty as string) as string
dim foo as new bar
dim theValue as string = "foo." & TheProperty
return theValue
End Function

If I do the following ...

GetThePropertyValueIWant("Name")

the function returns ...

foo.name

I want it to return George

and if I do this

GetThePropertyValueIWant("Job")
I want it to return Union Boss

Does that make more sense?

TIA

My Brain Hurts
Monday, October 07, 2002

Whoops - the class name in the above example should have been bar, the objec is foo.

My Brain Hurts
Monday, October 07, 2002

Public Function GetProp (name as String, o as Object) As String
  Return CType(o.GetType().GetProperty(name).GetValue(o, nothing), String)
End Function

Something like that should give you the results your looking for.

Michael Giagnocavo
Monday, October 07, 2002

Michael,

Can you hear that sound - it is all that built up pressure in my head being let out.

I was heading in the completely wrong direction.

Thanks  !!!!!!!!!

My Brain Hurts - no More!
Monday, October 07, 2002

*  Recent Topics

*  Fog Creek Home