473,385 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Property parameter?

Is there a way to pass a property around as a parameter without reflection?

My current motivation for this is the code below is testing multiple
properties to see that an exception is thrown if a user tries to set the
string value to an empty value. Ideally I'd like to have one method that
would take a Property as a parameter and test it:

[Test]
public void CompanyName_Error_CannotSetToEmpty() {
var customer = new Customer { ID = "TEST" };
try {
customer.CompanyName = string.Empty;
Assert.Fail("EmptyValueException for CompanyName property should
have been thrown.");
}
catch (EmptyValueException ex) {
_assertPropertyNameIsSetProperly(ex, "CompanyName");
}
}

[Test]
public void PhoneNumber_Error_CannotSetToEmpty() {
var customer = new Customer { ID = "TEST" };
try {
customer.Phone = string.Empty;
Assert.Fail("EmptyValueException for Phone property should have
been thrown.");
}
catch (EmptyValueException ex) {
_assertPropertyNameIsSetProperly(ex, "Phone");
}
}

private static void _assertPropertyNameIsSetProperly(EmptyValueExcepti on
ex, string expectedName) {
Assert.That(ex.PropertyName, Is.EqualTo(expectedName));
}
Sep 30 '08 #1
5 2005
On Sep 30, 2:36*pm, "Berryl Hesh" <efing...@yahoo.comwrote:
Is there a way to pass a property around as a parameter without reflection?
Not really - although you could pass round a delegate or pair of
delegates to get/set the property. That would be quite readable using
anonymous methods or lambda expressions.

Jon
Sep 30 '08 #2
can you please show me what that would look like?

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:97**********************************@8g2000hs e.googlegroups.com...
On Sep 30, 2:36 pm, "Berryl Hesh" <efing...@yahoo.comwrote:
Is there a way to pass a property around as a parameter without
reflection?
Not really - although you could pass round a delegate or pair of
delegates to get/set the property. That would be quite readable using
anonymous methods or lambda expressions.

Jon
Sep 30 '08 #3
On Sep 30, 2:45*pm, "Berryl Hesh" <efing...@yahoo.comwrote:
can you please show me what that would look like?
Which version of C# are you using?

Jon
Sep 30 '08 #4
3.5

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:37**********************************@m44g2000 hsc.googlegroups.com...
On Sep 30, 2:45 pm, "Berryl Hesh" <efing...@yahoo.comwrote:
can you please show me what that would look like?
Which version of C# are you using?

Jon
Sep 30 '08 #5
On Sep 30, 3:02*pm, "Berryl Hesh" <efing...@yahoo.comwrote:
3.5
Okay, I'll assume you mean you're using .NET 3.5 and C# 3.0. In which
case you can use lambda expressions:

public void SomeMethod<T>(Func<Customer,Tgetter, Action<Customer,T>
setter) {
Customer customer = ...;
// When you need to test the getter:
AssertEquals("", getter(customer)); // Or whatever

// When you need to test the setter
setter(customer, value);
}

Then to call SomeMethod:

SomeMethod(customer =customer.Phone, (customer, value) =>
customer.Phone = value);

Jon
Sep 30 '08 #6

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

Similar topics

2
by: Aaron | last post by:
Hi, I've seen javascript code where a constructor function is passed an argument "document", and inside the function itself the assignment "this.document = document;" is made. This is the code...
1
by: Heather | last post by:
How is the Parameter property for menu items used? Thanks! Heather
4
by: Roberto Sartori | last post by:
Hi. I'd want to know if it is possible in C # to declare one property analogous to following (written in VB): Property PropertyName (ByVal Index As Integer) As Object Get Return...
1
by: Andy G | last post by:
I've been getting this error all day. Could someone please look at my stored procedure and the code. I have a form that grabs and email address the user types in, calls a stored procedure with an...
6
by: Cc | last post by:
hi, is there a way to use byref on property set , because i would like to pass the value into the variable byref ?
4
by: Pritcham | last post by:
Hi all I've got a number of classes already developed (basic entity classes) like the following: Public Class Contact Private _firstname as String Private _age as Integer Public Property...
1
by: yeltsin27 | last post by:
I can see how to use a cookie, control property, form input value, profile, query string or session value as a parameter to a SQL Data Source. However I would like to use a property that is...
0
by: Bryce Fischer | last post by:
I've got a simple (I think) asp.net application. I've created a DataSet in App_Code/ItemDataSet.xsd. Tested connection, seemed to work fine. In my ASPX file, I first dropped an...
3
by: Peter Gast | last post by:
Hi, I need as a parameter for a control the names of my properties as a string. How can I get the name of the property as a string during runtime Example: Private _MyVar As Double Public...
7
by: Andy B | last post by:
I saw this in the set accessor of a property: Set(ByVal value As DataSet) What exactly does the stuff in the () mean? VS complained about it not being there when I took it out not knowing...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.