views:

375

answers:

4

I have 2 separate classes:

  • AreaProperties
  • FieldProperties

1 AreaProperties can map to 1 FieldProperties. Without changing the design, I want a method to return a List<> of these objects

What generic collection in C# would be suitable?

I understand I can send 2 Lists and the function would look like:

public List<AreaProperties> Save(ref List<FieldProperties>)
{
    ..code
}

EDIT: Dror Helper's solution sounds good. However, I recently found out that there is no 1:1 between FieldProperties and AreaProperties. How would I now handle this. I still want to go with a custom class that has an object of FieldProperties and AreaProperties but how would I handle the 1 to many scenario?

+6  A: 

You can create a class/struct that has two members - AreaProperties & FieldProperties and return a list of that class

class Pair<T1, T2>
{
     T1 t1;
     T2 t2;
}

List<Pair<AreaProperties, FieldProperties>> Save(){ ...}

Or use System.Collections.Generic.KeyValuePair instead (per Patrick suggestion below)

List<KeyValuePair<AreaProperties, FieldProperties>> Save(){ ... }

This way you keep the 1..1 relation as well.

Edit: In case you need 1..n relation I think you want to return List>> instead this way you have a list of Field Properties for each AreaProperties you get back and you still keep the relation between them.

Dror Helper
Shouldn't it be List<Pair> Save()?
Thomas Eyde
There's already a Generic struct in .NET, System.Collections.Generic.KeyValuePair(Of TKey, TValue)
Patrick McDonald
@Thomas Eyde It should be Pair<...> - thanks for the heads up
Dror Helper
@Patrick McDonald you're right, you could us KeyValuePair<T1, T2> instead
Dror Helper
@DH - Please see edit
DotnetDude
A: 

You can use a generic Dictionary<FieldProperties, AreaPropeties>? You will have to write your own Comparer<FieldPropeties> and KeyValuePair<FieldProperties, AreaProperties>.

Franci Penov
The other way around.
GoodEnough
Umh, why? He wants to map AreaProperties based on FieldProperties. seems to me FieldProperties should be the key, no?
Franci Penov
+1  A: 

You could return a List<KeyValuePair<AreaProperties, FieldProperties>>

Patrick McDonald
You beat me to it!
Mike_G
A: 

If the 1:1 relations needs to be enforced I recommend the struct method Dror mentioned. If the 1:1 relation is not enforced I'd consider using the out parameter modifier.

void foo() { List listArea; List listField; }

void Bar(out List listArea, out List listField) { listArea = new List(); listField = new List();

}

aef123