# Suggest how to extract method

I have 3 methods(* BindingSource are bindingsources, context is data context, cache* -are some List for cache operations):

 private void AddUpdateRowDocuments()
{
try
{
tRANSMITDOCDOCUMENTSRELATIONSBindingSource.EndEdit();
var t = tRANSMITDOCDOCUMENTSRELATIONSBindingSource.Current as WcfDataServiceReference.TRANSMIT_DOC_DOCUMENTS_RELATIONS;
if (t.TRANSMIT_DOC_ID == 0)
{
if (!cachetddrList.Contains(t))
{
}
return;
}
context.UpdateObject(t);
}
catch (Exception ex)
{
logger.ErrorException(string.Empty, ex);
}
}
{
try
{
tRANSMITDOCORGANIZATIONRELATIONSBindingSource.EndEdit();
var t = tRANSMITDOCORGANIZATIONRELATIONSBindingSource.Current as WcfDataServiceReference.TRANSMIT_DOC_ORGANIZATION_RELATIONS;
if (t.TRANSMIT_DOC_ID == 0)
{
if (!cachetdorList.Contains(t))
{
}
return;
}
context.UpdateObject(t);
}
catch (Exception ex)
{
logger.ErrorException(string.Empty, ex);
}
}
{
try
{
tRANSMITDOCPARTATORRELATIONSBindingSource.EndEdit();
var t = tRANSMITDOCPARTATORRELATIONSBindingSource.Current as WcfDataServiceReference.TRANSMIT_DOC_PARTATOR_RELATIONS;
if (t.TRANSMIT_DOC_ID == 0)
{
if (!cachetdprList.Contains(t))
{
}
return;
}
context.UpdateObject(t);
}
catch (Exception ex)
{
logger.ErrorException(string.Empty, ex);
}
}


Any thoughts on how to improve them? My inner sense says that they can be turned into one generic method, but i have little experience with generics, so cant say if i am right.

Something along the lines of this. Generics aren't that difficult. Just replace every occurance of 'some desired type' with the generic identifier.

private void AddUpdateRow<T>(BindingSource bindingSource, List<T> cachedList)
where T : ITransmitDocId
{
try
{
bindingSource.EndEdit();
T t = bindingSource.Current as T;
if (t.TRANSMIT_DOC_ID == 0)
{
if (!cachedList.Contains(t))
{
}
return;
}
context.UpdateObject(t);
}
catch (Exception ex)
{
logger.ErrorException(string.Empty, ex);
}
}


The where constraint in the function definition should indicate a common interface/class in which TRANSMIT_DOC_ID is defined.

• problem with t.TRANSMIT_DOC_ID: 'T' does not contain a definition for 'TRANSMIT_DOC_ID' and no extension method 'TRANSMIT_DOC_ID' accepting a first argument of type 'T' could be found (are you missing a using directive or an assembly reference?) – 0x49D1 Mar 2 '11 at 14:52
• @nihi_l_ist: How are you calling this method? Does the error occur in the generic function or on the caller? Be sure to replace T with your desired type when calling the function. E.g. AddUpdateRow<CourtWcfDataServiceReference.TRANSMIT_DOC_ORGANIZATION_RELATIONS>( bindingSource, cachedList ). – Steven Jeuris Mar 2 '11 at 14:55
• It appears in generic method(code does't even compile)..Think T must implement some interface that should be common for all that classes(like TRANSMIT_DOC_ORGANIZATION_RELATIONS and ets..) – 0x49D1 Mar 2 '11 at 14:58
• Ok...thank you anyway .. I'll move that t.TRANSMIT_DOC_ID to method's parameters and that all..Code will become a bit wired, but it WILL wwork :) thanks for the answer. – 0x49D1 Mar 2 '11 at 15:03
• @nihi_l_ist: oh I see the problem now! you can add a where clause to the function ... to indicate T is at least an interface you define which defines TRANSMIT_DOC_ID. WIll update the answer. – Steven Jeuris Mar 2 '11 at 15:06