6
\$\begingroup\$

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))
 {
 cachetddrList.Add(t);
 }
 return;
 }
 context.UpdateObject(t);
 }
 catch (Exception ex)
 {
 logger.ErrorException(string.Empty, ex);
 }
 }
private void AddUpdateRowOrganizations()
 {
 try
 {
 tRANSMITDOCORGANIZATIONRELATIONSBindingSource.EndEdit();
 var t = tRANSMITDOCORGANIZATIONRELATIONSBindingSource.Current as WcfDataServiceReference.TRANSMIT_DOC_ORGANIZATION_RELATIONS;
 if (t.TRANSMIT_DOC_ID == 0)
 {
 if (!cachetdorList.Contains(t))
 {
 cachetdorList.Add(t);
 }
 return;
 }
 context.UpdateObject(t);
 }
 catch (Exception ex)
 {
 logger.ErrorException(string.Empty, ex);
 }
 }
private void AddUpdateRowPartators()
 {
 try
 {
 tRANSMITDOCPARTATORRELATIONSBindingSource.EndEdit();
 var t = tRANSMITDOCPARTATORRELATIONSBindingSource.Current as WcfDataServiceReference.TRANSMIT_DOC_PARTATOR_RELATIONS;
 if (t.TRANSMIT_DOC_ID == 0)
 {
 if (!cachetdprList.Contains(t))
 {
 cachetdprList.Add(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.

asked Mar 2, 2011 at 14:39
\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

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))
 {
 cachedList.Add(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.

answered Mar 2, 2011 at 14:48
\$\endgroup\$
6
  • \$\begingroup\$ 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?) \$\endgroup\$ Commented Mar 2, 2011 at 14:52
  • \$\begingroup\$ @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 ). \$\endgroup\$ Commented Mar 2, 2011 at 14:55
  • \$\begingroup\$ 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..) \$\endgroup\$ Commented Mar 2, 2011 at 14:58
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Mar 2, 2011 at 15:03
  • \$\begingroup\$ @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. \$\endgroup\$ Commented Mar 2, 2011 at 15:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.