|
-
May 23rd, 2005, 02:32 PM
#1
Thread Starter
Hyperactive Member
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
-
May 23rd, 2005, 04:47 PM
#2
Frenzied Member
Re: how retreive the value of a property from object when propertyname in a variable ?
Try something like this
VB Code:
Public Class myClass
Private myProperty As Integer
Public Function get_Datarow(ByVal field_list As ArrayList)
While en.MoveNext
Console.WriteLine( Me.[en.Current] )
End While
End Function
Public Property myPropertyName() As Integer
Get
Return myProperty
End Get
Set (ByVal Value As Integer)
myProperty = Value
End Set
End Property
End Class
Being educated does not make you intelligent.
Need a weekend getaway??? Come Visit
-
May 23rd, 2005, 04:58 PM
#3
Thread Starter
Hyperactive Member
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!
-
May 24th, 2005, 01:05 PM
#4
I wonder how many charact
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();
}
}
-
May 24th, 2005, 01:20 PM
#5
I wonder how many charact
Re: how retreive the value of a property from object when propertyname in a variable ?
Here's a VB version
VB Code:
Public Class SomeClass
Public Sub SomeClass()
End Sub
Private _level As Integer = 3
Public Property Level()
Get
Return _level
End Get
Set(ByVal Value)
_level = Value
End Set
End Property
Public Shared Sub Main()
Dim propertyname As String = "Level"
Dim type As Type
'instantiate an instance of SomeClass
Dim someClass As SomeClass = New SomeClass
'Get the type of the SomeClass class.
type = GetType(SomeClass)
'Use the Reflection namespace PropertyInfo class to get the reflected info
Dim info As System.Reflection.PropertyInfo = type.GetProperty(propertyname)
'if the property has a set accessor
If (info.CanWrite) Then
'call our instance 'someClass', setting its Level property to 10.
info.SetValue(someClass, 10, Nothing)
End If
'display to console to confirm it was assigned
Console.WriteLine(someClass.Level.ToString())
Console.ReadLine()
End Sub
End Class
Last edited by nemaroller; May 24th, 2005 at 01:36 PM.
-
May 24th, 2005, 01:30 PM
#6
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|