We help IT Professionals succeed at work.

remove repetitive code / pass property address into function

Ryan
Ryan asked
on
Medium Priority
297 Views
Last Modified: 2012-08-22
I have the following code in a n-tier app, which is repeated about 40 times in my using a different function in DA.Components, and I'm wondering if there's a cleaner way to do it.  

NewECNs is populated within the function call, and is normally empty, and may eventually get up to a count of 10.

Dim NewECNs As New List(Of String)
lblTypeLabel.Text = DA.Components.GetArrTypeLabelPN(arrFamily, _EffectiveDate, NewECNs)
For Each s As String In NewECNs
	ECNs.Add(New ECN(s, "Type Label"))
Next

Open in new window


My current thinking is if I could pass an address of the property ECN as list(of string), then I could remove all but the single function call, but I don't think I can do this? Any other ideas?
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Not sure what you are doing there. Is the function call returning a string as well as populating a parameter?

Why not send the ECNs to the function call?

How about replacing

Dim NewECNs As New List(Of String)
lblTypeLabel.Text = DA.Components.GetArrTypeLabelPN(arrFamily, _EffectiveDate, NewECNs)
For Each s As String In NewECNs
      ECNs.Add(New ECN(s, "Type Label"))
Next

with

Dim NewECNs As New List(Of String)
lblTypeLabel.Text = DA.Components.GetArrTypeLabelPN(arrFamily, _EffectiveDate, NewECNs)
ECNs.AddRange(NewECNs.ToArray())
Project Engineer, Electrical
CERTIFIED EXPERT
Commented:
ECN is a Business Object (namespace), where as the DA.Components is a Data Accessor  (namespace).  DAs shouldn't see BOs in N-Tier, which is why I can't pass ECN into the function, and why I'm passing in a generic and then parsing the generic in my BO class.

Also notice I'm storing a label in this case "Type Label" with the ECN, not just a string.

Just being greedy and trying to get it all in 1 clean line.

I ended up passing a delegate sub to the DA.Components functions, which resides in the BO.

In DA Namespace
 
Public Shared Function GetArrTypeLabelPN(ByVal ArrType As String, ByVal EffectiveDate As Date, ByVal AddECN As AddECN) As String
     Return GetKeyValue("ArrTypeLabel" & ArrType, EffectiveDate, AddECN, "Type Label").ToString
End Function

Public Delegate Sub AddECN(ByVal ECN As String, ByVal ItemType As String)

Open in new window


In BO Namepace
retVal.Add(New LineItem(8, DA.Components.GetArrTypeLabel("ArrTypeLabel" & arrFamily, _EffectiveDate, AddressOf AddECN).ToString(), "1"))


Public Sub AddECN(ByVal ECN As String, ByVal ItemType As String)
	_ECNs.Add(New ECN(ECN, ItemType))
End Sub

Open in new window


Still seems a little bastardized to pass the address of a function in BO to DA.
RyanProject Engineer, Electrical
CERTIFIED EXPERT

Author

Commented:
No other solution was provided.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.