Results 1 to 6 of 6

Thread: how retreive the value of a property from object when propertyname in a variable ?

  1. #1

    Thread Starter
    Hyperactive Member fabianus's Avatar
    Join Date
    Sep 2004
    Location
    Paris
    Posts
    402

    Question how retreive the value of a property from object when propertyname in a variable ?

    how my I retreive the value of a property in an object when I stored the propertyname in a variable ?

    I give you a little example of what I want to do. I wrote "Me.[en.Current]" for what I am looking for - how is the correct syntax ?

    Public Class myClass

    Dim myProperty As Integer

    Public Function get_Datarow(ByVal field_list As ArrayList)
    While en.MoveNext
    Console.WriteLine( Me.[en.Current] )
    End While
    End Function
    End Class

    Thank you very much!
    Fabian

  2. #2
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861

    Re: how retreive the value of a property from object when propertyname in a variable ?

    Try something like this
    VB Code:
    1. Public Class myClass
    2.  
    3.    Private myProperty As Integer
    4.  
    5.    Public Function get_Datarow(ByVal field_list As ArrayList)
    6.       While en.MoveNext
    7.          Console.WriteLine( Me.[en.Current] )
    8.       End While
    9.    End Function
    10.  
    11.    Public Property myPropertyName() As Integer
    12.       Get
    13.          Return myProperty
    14.       End Get
    15.       Set (ByVal Value As Integer)
    16.          myProperty = Value
    17.       End Set
    18.    End Property
    19. End Class
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  3. #3

    Thread Starter
    Hyperactive Member fabianus's Avatar
    Join Date
    Sep 2004
    Location
    Paris
    Posts
    402

    Question Re: how retreive the value of a property from object when propertyname in a variable

    Hi Memnoch1207,

    thank you for your answer.

    I tried to understand... but I did not. Could you give me some more indication?

    As you see I wrote "Me.[en.Current]" for the part that is missing to me.

    I want to call a property from an object, but I have stored the propertyname in a variable.

    This means I can not write

    myClass.myPropertyName

    because "myPropertyName" is stored in ma variable ! But I don't know the syntax...

    of course if I wirte

    myVariable = "myPropertyName"
    Console.WriteLine(myClass.myVariable)

    it doesn't work, because myVariable is not a proberty of myClass.

    Thank you very much for some more help!

  4. #4
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    Re: how retreive the value of a property from object when propertyname in a variable ?

    You will need Reflection to do this. You can find more information using MSDN and search for PropertyInfo.

    Code:
    //c#
    	class SomeClass
    	{
    		public SomeClass(){}
    		private int _level = 3;
    		public int Level
    		{
    			get
    			{
    				return _level;
    			}
    			set
    			{
    				_level = value;
    			}
    		}
    
    		/// <summary>
    		/// The main entry point for the application.
    		/// </summary>
    		[STAThread]
    		static void Main(string[] args)
    		{
    			//assign our propertyname to a string variable
    			string propertyname = "Level";
    			Type type;
    			//instantiate an instance of SomeClass
    			SomeClass someClass = new SomeClass();
    			//Get the type of the SomeClass class.
    			type = typeof(SomeClass);
    
    
    			//Use the Reflection namespace PropertyInfo class to get the reflected info
    			System.Reflection.PropertyInfo info = type.GetProperty(propertyname);
    			//if the property has a get accessor
    			if (info.CanWrite )
    			{
    				//call our instance 'someClass', setting its Level property to 10.
    				info.SetValue(someClass,10,null);
    			}
    
    			//display to console to confirm it was assigned
    			Console.WriteLine(someClass.Level.ToString());
    			Console.ReadLine();
    		
    		}
    	}

  5. #5
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    Re: how retreive the value of a property from object when propertyname in a variable ?

    Here's a VB version
    VB Code:
    1. Public Class SomeClass
    2.  
    3.     Public Sub SomeClass()
    4.  
    5.     End Sub
    6.  
    7.     Private _level As Integer = 3
    8.  
    9.     Public Property Level()
    10.         Get
    11.             Return _level
    12.         End Get
    13.         Set(ByVal Value)
    14.             _level = Value
    15.         End Set
    16.     End Property
    17.  
    18.     Public Shared Sub Main()
    19.  
    20.         Dim propertyname As String = "Level"
    21.         Dim type As Type
    22.  
    23.         'instantiate an instance of SomeClass
    24.         Dim someClass As SomeClass = New SomeClass
    25.  
    26.         'Get the type of the SomeClass class.
    27.         type = GetType(SomeClass)
    28.  
    29.  
    30.         'Use the Reflection namespace PropertyInfo class to get the reflected info
    31.         Dim info As System.Reflection.PropertyInfo = type.GetProperty(propertyname)
    32.  
    33.         'if the property has a set accessor
    34.         If (info.CanWrite) Then
    35.  
    36.             'call our instance 'someClass', setting its Level property to 10.
    37.             info.SetValue(someClass, 10, Nothing)
    38.  
    39.         End If
    40.  
    41.         'display to console to confirm it was assigned
    42.         Console.WriteLine(someClass.Level.ToString())
    43.         Console.ReadLine()
    44.  
    45.     End Sub
    46. End Class
    Last edited by nemaroller; May 24th, 2005 at 01:36 PM.

  6. #6

    Thread Starter
    Hyperactive Member fabianus's Avatar
    Join Date
    Sep 2004
    Location
    Paris
    Posts
    402

    Resolved Re: how retreive the value of a property from object when propertyname in a variable

    that's quite something :-)

    absolutly great - thank you a lot, I could have search for this for an eternity


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width