By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,894 Members | 2,193 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,894 IT Pros & Developers. It's quick & easy.

Accesing property name at design times

P: n/a
Hello Guys,

Please look at the code sample below.

Class Customer
{
private string _ID;
private string _Name;

public string ID
{
get { return _ID; }
set { _ID = value; }
}

public string Name
{
get { return _Name; }
set { _ID = Name; }
}
}
void SomeFunction(Customer, PropertyName as String)
{
does something
}

============
In UI (Design Time): A different application that using the above
assembly

psv main()
{
Customer myC = new Customer();
myC.ID = "1";
myC.Name = "John Doe";

SomeFunction(Customer, "ID");

// Instead of the above statement I want something like
SomeFunction(Customer, Customer.Properties.ID);
}

What are the strategies to make this possible?

I know of one using an inner class. It is outlined below. In class
customer add the following:
public class Properties
{
public const string ID = "ID";
public const string Name = "Name";
}

Thanks

Sep 29 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

You could use an enumeration, e.g.

enum Properties
{
ID, Name
}

sr*******@gmail.com wrote:
Hello Guys,

Please look at the code sample below.

Class Customer
{
private string _ID;
private string _Name;

public string ID
{
get { return _ID; }
set { _ID = value; }
}

public string Name
{
get { return _Name; }
set { _ID = Name; }
}
}
void SomeFunction(Customer, PropertyName as String)
{
does something
}

============
In UI (Design Time): A different application that using the above
assembly

psv main()
{
Customer myC = new Customer();
myC.ID = "1";
myC.Name = "John Doe";

SomeFunction(Customer, "ID");

// Instead of the above statement I want something like
SomeFunction(Customer, Customer.Properties.ID);
}

What are the strategies to make this possible?

I know of one using an inner class. It is outlined below. In class
customer add the following:
public class Properties
{
public const string ID = "ID";
public const string Name = "Name";
}

Thanks
Sep 29 '06 #2

P: n/a
Here is snipplet.

object can be anything.

This code will take 1 object, instantiate another object, and if the
property names are the same, it will set the value of the second object to
objects one value.
Notice there is no case switching on type.

It assumes if the property name match, the datatype match.
object1 = new SomeObject();

destinationObject = new SomeObject();

PropertyInfo[] props =
object1.GetType().GetProperties();

//the next loop is basically a cloning mechanism.
foreach (PropertyInfo prop in props )
{
if (prop.CanWrite) // only update properties which can be written to
{

PropertyInfo indivReportProperty =
destinationObject.GetType().GetProperty(prop.Name) ;
if (null != indivReportProperty) //handle the case where the objects
are not perfect copies of one another
{
indivReportProperty.SetValue (destinationObject ,
(prop.GetValue(object1, null)) , null);
}

}
}
Sep 29 '06 #3

P: n/a
We cannot use an enumeration because it evaluates to a number. It
should evaluate to a string that represents the property name.
Chris Fulstow wrote:
You could use an enumeration, e.g.

enum Properties
{
ID, Name
}

sr*******@gmail.com wrote:
Hello Guys,

Please look at the code sample below.

Class Customer
{
private string _ID;
private string _Name;

public string ID
{
get { return _ID; }
set { _ID = value; }
}

public string Name
{
get { return _Name; }
set { _ID = Name; }
}
}
void SomeFunction(Customer, PropertyName as String)
{
does something
}

============
In UI (Design Time): A different application that using the above
assembly

psv main()
{
Customer myC = new Customer();
myC.ID = "1";
myC.Name = "John Doe";

SomeFunction(Customer, "ID");

// Instead of the above statement I want something like
SomeFunction(Customer, Customer.Properties.ID);
}

What are the strategies to make this possible?

I know of one using an inner class. It is outlined below. In class
customer add the following:
public class Properties
{
public const string ID = "ID";
public const string Name = "Name";
}

Thanks
Sep 29 '06 #4

P: n/a
Your example is a nice one. But it does not address the issue here at
hand.

I want to be table to access the property names ( not values) at DESIGN
time.

I humbly suggest you reread my original post.

sloan wrote:
Here is snipplet.

object can be anything.

This code will take 1 object, instantiate another object, and if the
property names are the same, it will set the value of the second object to
objects one value.
Notice there is no case switching on type.

It assumes if the property name match, the datatype match.
object1 = new SomeObject();

destinationObject = new SomeObject();

PropertyInfo[] props =
object1.GetType().GetProperties();

//the next loop is basically a cloning mechanism.
foreach (PropertyInfo prop in props )
{
if (prop.CanWrite) // only update properties which can be written to
{

PropertyInfo indivReportProperty =
destinationObject.GetType().GetProperty(prop.Name) ;
if (null != indivReportProperty) //handle the case where the objects
are not perfect copies of one another
{
indivReportProperty.SetValue (destinationObject ,
(prop.GetValue(object1, null)) , null);
}

}
}
Sep 29 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.