Click here to Skip to main content
16,009,640 members
Home / Discussions / C#
   

C#

 
QuestionRadioButtonList Control - Problems getting value from listItems programmatically added - HELP! Pin
Alan R. Smith12-Jan-06 4:43
Alan R. Smith12-Jan-06 4:43 
QuestionFinding strings in a project Pin
MartinSmith12-Jan-06 3:51
MartinSmith12-Jan-06 3:51 
QuestionMapping a Drive at runtime Pin
exhaulted12-Jan-06 2:49
exhaulted12-Jan-06 2:49 
AnswerRe: Mapping a Drive at runtime Pin
Koushik Biswas12-Jan-06 6:07
Koushik Biswas12-Jan-06 6:07 
GeneralRe: Mapping a Drive at runtime Pin
exhaulted12-Jan-06 9:08
exhaulted12-Jan-06 9:08 
Questionproblem with datagrid.unselect Pin
melanieab12-Jan-06 2:41
melanieab12-Jan-06 2:41 
AnswerRe: problem with datagrid.unselect Pin
exhaulted12-Jan-06 3:17
exhaulted12-Jan-06 3:17 
QuestionGenerics Problem Pin
Kevin McFarlane12-Jan-06 1:41
Kevin McFarlane12-Jan-06 1:41 
Is there a way using .NET Generics of providing a C# equivalent to the following C++?

<pre>
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>

using namespace std;

class Employee
{
public:
Employee(int id, const string& name) : _id(id), _name(name) {}
int id() const { return _id; }
string name() const { return _name; }
private:
int _id;
string _name;
};

// Employees are equal if they share the same name.
class EmployeeEqualTo
{
public:
EmployeeEqualTo(string name) : _name(name) {}
bool operator()(const Employee& employee) const
{ return employee.name() == _name; }
private:
string _name;
};

void Run()
{
vector<Employee> employees;
employees.push_back(Employee(1, "Peter"));
employees.push_back(Employee(2, "Paul"));
employees.push_back(Employee(3, "Helen"));

vector<Employee>::const_iterator it;
string match;

// Find Paul
match = "Paul";
it = find_if(employees.begin(), employees.end(), EmployeeEqualTo(match));
cout << "Name: " << it->name() << ", id: " << it->id() << "\n";

// Find Helen
match = "Helen";
it = find_if(employees.begin(), employees.end(), EmployeeEqualTo(match));
cout << "Name: " << it->name() << ", id: " << it->id() << "\n";
}


int _tmain(int argc, _TCHAR* argv[])
{
Run();
return 0;
}
</pre>

The key thing here is that we just need to define a single function object EmployeeEqualTo to find an employee that is equal to a match that we specify.

Now consider a C# generics attempt.

<pre>
class Employee
{
public Employee(int id, string name)
{
_id = id;
_name = name;
}

private int _id;
public int ID
{
get { return _id; }
}

private string _name;
public string Name
{
get { return _name; }
}
}

class EmployeeTester
{
public void Run()
{
List<Employee> employees = new List<Employee>();
employees.Add(new Employee(1, "Peter"));
employees.Add(new Employee(2, "Paul"));
employees.Add(new Employee(3, "Helen"));
Employee employee;

// Find Paul
employee = employees.Find(EmployeeEqualToPaul);
Console.WriteLine("Name = {0}, ID = {1}", employee.Name, employee.ID);

// Find Helen
employee = employees.Find(EmployeeEqualToHelen);
Console.WriteLine("Name = {0}, ID = {1}", employee.Name, employee.ID);
}

/// <summary>
/// Is employee Paul?
/// </summary>
private bool EmployeeEqualToPaul(Employee employee)
{
return employee.Name == "Paul";
}

/// <summary>
/// Is employee Helen?
/// </summary>
private bool EmployeeEqualToHelen(Employee employee)
{
return employee.Name == "Helen";
}
}
</pre>

We have to define two predicates, EmployeeEqualToPaul and EmployeeEqualToHelen to provide the same functionality. Is there a way of defining a single predicate in this case?



Kevin
AnswerRe: Generics Problem Pin
mav.northwind12-Jan-06 5:18
mav.northwind12-Jan-06 5:18 
GeneralRe: Generics Problem Pin
Kevin McFarlane12-Jan-06 7:44
Kevin McFarlane12-Jan-06 7:44 
Questionplease correct my query Pin
Sasuko12-Jan-06 1:18
Sasuko12-Jan-06 1:18 
AnswerRe: please correct my query Pin
Colin Angus Mackay12-Jan-06 1:48
Colin Angus Mackay12-Jan-06 1:48 
GeneralRe: please correct my query Pin
Sasuko12-Jan-06 2:40
Sasuko12-Jan-06 2:40 
GeneralRe: please correct my query Pin
Colin Angus Mackay12-Jan-06 2:50
Colin Angus Mackay12-Jan-06 2:50 
AnswerRe: please correct my query Pin
Guffa12-Jan-06 2:23
Guffa12-Jan-06 2:23 
AnswerRe: please correct my query Pin
Sasuko12-Jan-06 4:49
Sasuko12-Jan-06 4:49 
QuestionUsing String.Replace method with variables Pin
sohne11-Jan-06 23:11
sohne11-Jan-06 23:11 
AnswerRe: Using String.Replace method with variables Pin
Colin Angus Mackay11-Jan-06 23:23
Colin Angus Mackay11-Jan-06 23:23 
QuestionImporting contacts to Microsoft Outlook with C# Pin
Starchild200511-Jan-06 21:59
Starchild200511-Jan-06 21:59 
QuestionHow can be a Transparent panel control can be created Pin
Shashidharreddy11-Jan-06 21:52
Shashidharreddy11-Jan-06 21:52 
AnswerRe: How can be a Transparent panel control can be created Pin
AB777111-Jan-06 23:53
AB777111-Jan-06 23:53 
AnswerRe: How can be a Transparent panel control can be created Pin
exhaulted12-Jan-06 3:24
exhaulted12-Jan-06 3:24 
Questionestablish GPRS connection Pin
Frank23111-Jan-06 20:58
Frank23111-Jan-06 20:58 
Questionwindows services Pin
PrakashBhaskar11-Jan-06 19:42
PrakashBhaskar11-Jan-06 19:42 
AnswerRe: windows services Pin
Mike Dimmick11-Jan-06 23:56
Mike Dimmick11-Jan-06 23:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.