reusing Code

T

tshad

I have some code that is exactly the same except for 1 name.

In the "for loops" below the 2nd parameters change as they are pointing to
separate string arrays in the structure:

nhDataBean.benefitGroupCodeList.GetUpperBound(0)
nhDataBean.jobCodeList.GetUpperBound(0)

***************************************************************
public DataTable GetBenefits()
{
int ktr1 = 0;
DataTable dt = new DataTable();
DataRow dr;
// Add the columns to the DataTable's Columns collection
dt.Columns.Add("code");
dt.Columns.Add("description");

for (ktr1=1;ktr1<=nhDataBean.benefitGroupCodeList.GetUpperBound(0);ktr1++)
{
dr = dt.NewRow();
dr["code"] = nhDataBean.benefitGroupCodeList[ktr1][0];
dr["description"] = nhDataBean.benefitGroupCodeList[ktr1][1];
dt.Rows.Add(dr);
}
return dt;
}

public DataTable GetJobTitles()
{
int ktr1 = 0;
DataTable dt = new DataTable();
DataRow dr;
// Add the columns to the DataTable's Columns collection
dt.Columns.Add("code");
dt.Columns.Add("description");

for (ktr1=0;ktr1<=nhDataBean.jobCodeList.GetUpperBound(0);ktr1++)
{
dr = dt.NewRow();
dr["code"] = nhDataBean.jobCodeList[ktr1][0];
dr["description"] = nhDataBean.jobCodeList[ktr1][1];
dt.Rows.Add(dr);
}
return dt;
}
**********************************************************************

There are about 10 of these in the structure.

Is there a way to pass this into one function change the for loop to use the
variable passed in?

Thanks,

Tom
 
O

Otis Mukinfus

I have some code that is exactly the same except for 1 name.

In the "for loops" below the 2nd parameters change as they are pointing to
separate string arrays in the structure:

nhDataBean.benefitGroupCodeList.GetUpperBound(0)
nhDataBean.jobCodeList.GetUpperBound(0)

***************************************************************
public DataTable GetBenefits()
{
int ktr1 = 0;
DataTable dt = new DataTable();
DataRow dr;
// Add the columns to the DataTable's Columns collection
dt.Columns.Add("code");
dt.Columns.Add("description");

for (ktr1=1;ktr1<=nhDataBean.benefitGroupCodeList.GetUpperBound(0);ktr1++)
{
dr = dt.NewRow();
dr["code"] = nhDataBean.benefitGroupCodeList[ktr1][0];
dr["description"] = nhDataBean.benefitGroupCodeList[ktr1][1];
dt.Rows.Add(dr);
}
return dt;
}

public DataTable GetJobTitles()
{
int ktr1 = 0;
DataTable dt = new DataTable();
DataRow dr;
// Add the columns to the DataTable's Columns collection
dt.Columns.Add("code");
dt.Columns.Add("description");

for (ktr1=0;ktr1<=nhDataBean.jobCodeList.GetUpperBound(0);ktr1++)
{
dr = dt.NewRow();
dr["code"] = nhDataBean.jobCodeList[ktr1][0];
dr["description"] = nhDataBean.jobCodeList[ktr1][1];
dt.Rows.Add(dr);
}
return dt;
}
**********************************************************************

There are about 10 of these in the structure.

Is there a way to pass this into one function change the for loop to use the
variable passed in?

Thanks,

Tom
Yes you can. Just substitute your variable(s) in the appropriate places.

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 
K

Kevin Spencer

Identify the elements in the code which are different for each usage, and
create a function that takes these elements as parameters.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top