If this sounds too impossible, pls dont down vote 🙂 Just my quirk that I wonder if ever I can pass a property as the parameter to a function to get some result.
For eg.
void delete_button_click ()
{
foreach (DataGridViewRow row in dgvRecords.SelectedRows)
dgvRecords.Rows.Remove(row);
}
And suppose
void delete_all_button click ()
{
foreach (DataGridViewRow row in dgvRecords.Rows)
dgvRecords.Rows.Remove(row);
}
Can I write the above two codes in one function since the only difference is SelectedRows and Rows. Something like this:
void button_click (/*DataGridView property rows*/)
{
foreach (DataGridViewRow row in dgvRecords.rows)
dgvRecords.Rows.Remove(row);
}
And call it
{
button_click(DataGridView.Rows);
//and
button_click(DataGridView.SelectedRows);
}
Another eg.,
decimal GetAmount1(User usr)
{
decimal sum = 0;
foreach (DataGridViewRow row in dgvRecords.Rows)
{
Tuple<User, User, Notification, Acknowledgment> tup = (Tuple<User, User, Notification, Acknowledgment>)row.Tag;
if (tup.Item1 == usr)
sum += tup.Item4.Sum;
}
return sum;
}
and
decimal GetAmount2(User usr)
{
decimal sum = 0;
foreach (DataGridViewRow row in dgvRecords.Rows)
{
Tuple<User, User, Notification, Acknowledgment> tup = (Tuple<User, User, Notification, Acknowledgment>)row.Tag;
if (tup.Item2 == usr)
sum += tup.Item4.Sum;
}
return sum;
}
The only difference in above codes are just a property. So is this available:
decimal GetAmount(User usr, Tuple<User, User, Notification, Acknowledgment>.Property Item)
{
decimal sum = 0;
foreach (DataGridViewRow row in dgvRecords.Rows)
{
Tuple<User, User, Notification, Acknowledgment> tup = (Tuple<User, User, Notification, Acknowledgment>)row.Tag;
if (tup.Item == usr)
sum += tup.Item4.Sum;
}
return sum;
}
And call :
{
GetAmount(usr, Tuple<User, User, Notification, Acknowledgment>.Item1)
//and
GetAmount(usr, Tuple<User, User, Notification, Acknowledgment>.Item2)
}
??
Your first example is can be solved by just using a normal parameter:
The signature of a method to remove rows would be:
and call it with:
The second example requires the use of a
Func<T, TResult>which allows you to define what property you want to select.So your
GetAmount()method siganture becomes:and you would call it: