Reputation:
learning c# on my own , in a new project i have started working on,
one of the methods, accepts a List<string>
type data, passed in as a parameter
. i would like to know , now ...that the need is for two of those lists, two types of data, or two groups,
and just for this example , say i have a pros and cons, or.. girls and boys,
these are actually 2 separated lists of elements or objects, i need to pass in, as a parameter
, and i want it to be passed as-one data-Type, and then, inside the method i will take care of separation of data lists , so.. instead of a couple of List
objects i, thought(for a second there , that a Dictionary
will be suitable...)
though i will only have one element (one item... each is a List
) ,in this data type that i need to pass
what can i do to get this result ?
i will try to illustrate it :
List<string> classGirls = new List<string>();
List<string> classBoys = new List<string>();
for eace item in source... load both from the source
done List girls + List Boys are populated how would you pass them as one as a Dictionary Could though knowing you will only have One Girls And One Boys ListObjects ?
public bool foundAMatch( string Lookup, List<string> G , List<string> B){
{
var firstGirl = G.ElementAt(0);
var firstBoy = B.ElementAt(0);
return firstGirl == Lookup && firstBoy !=Lookup ;
}
instead i need it to be something Like
public bool foundAmatch(string Lookup, someDataType.... Kids)
{
var firstGirl = kids-girls <-- first entity in kids;
var firstBoy = kids-boys <-- first entity in Kids;
return firstGirl == Lookup && firstBoy !=Lookup ;
}
few things in mind ...as to properties of data , it should have good performance... naturally desired, though most importantly, it should be appropriate /easy to arrange and suitable for iterations using loops for sorting and as subject to all kind of statistic operations .
how will you implement the logic , is it an existing data Type... that i am thinking of ?
if it's not, what approach will you implement in this scenario choosing / crafting a data Type ?
Upvotes: 2
Views: 1167
Reputation: 324
The best way to get one collection is to create a class representing a boy as well as a girl. Please find the example below:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private class Kid
{
public bool Gender { get; set; }
public String Name { get; set; }
}
private List<Kid> kids;
private void Form1_Load(object sender, EventArgs e)
{
// === Load data =================================================
kids = new List<Kid>();
kids.Add(new Kid { Name = "John", Gender = true });
kids.Add(new Kid { Name = "Paul", Gender = true });
kids.Add(new Kid { Name = "Jack", Gender = true });
kids.Add(new Kid { Name = "Brenda", Gender = false });
kids.Add(new Kid { Name = "Judith", Gender = false });
kids.Add(new Kid { Name = "Sofia", Gender = false });
Kid foundKid = foundAMatch("sofIa");
}
private Kid foundAMatch(string name)
{
var result = (from K in kids
where K.Name.ToLower() == name.ToLower()
select K);
if (result.Count() != 0)
return result.First();
else
return null;
}
private Kid foundAMatch(string name, bool gender)
{
var result = (from K in kids
where K.Name.ToLower() == name.ToLower() && K.Gender == gender
select K);
if (result.Count() != 0)
return result.First();
else
return null;
}
}
}
Upvotes: 0
Reputation: 3105
You can solve this easily by using inheritance:
public abstract class Person
{
public string Name { get; private set; } // The setter is private because we only have to set this name when we create an instance
protected Person(string name)
{
Name = name;
}
}
public class Male : Person
{
public Male(string name) : base(name) // This constructor calls the constructor of the class it inherits and passes on the same argument
}
public class Female : Person
{
public Female(string name) : base(name)
}
public bool IsMatch(string needle, IEnumerable<Person> haystack)
{
var firstGirl = haystack.OfType<Female>().FirstOrDefault();
var firstBuy = haystack.OfType<Male>().FirstOrDefault();
return firstGirl != null &&
firstGirl.Name == needle &&
firstBoy != null &&
firstBoy.Name != needle;
}
edit:
I quite like extension methods, so I'd write the method like this:
public static class PersonExtensions
{
public static bool IsMatch(this IEnumerable<Person> haystack, string needle)
{
// same method logic in here
}
}
which you can then use like:
var people = new List<Person>();
people.Add(new Male { Name = "Bob" });
people.Add(new Female { Name = "Mary" });
var isMatch = people.IsMatch("Jane");
edit2:
It's probably even better to just have gender as a property of the Person
class:
public enum Sex
{
Male,
Female
}
public class Person
{
public string Name { get; private set; }
public Sex Gender { get; private set; }
public Person(string name, Sex gender)
{
Name = name;
Gender = gender;
}
}
and change the method to:
var firstGirl = haystack.FirstOrDefault(p => p.Gender == Gender.Female);
var firstBoy = haystack.FirstOrDefault(p => p.Gender == Gender.Male);
Upvotes: 3