Combine functions into 1

T

tshad

I have about 10 functions that are identical except for one variable name
that I am using in my "for" loop:

for (ktr1=1;ktr1<=nhDataBean.projectCodeList.GetUpperBound(0);ktr1++)
for (ktr1=1;ktr1<=nhDataBean.shiftCodeList.GetUpperBound(0);ktr1++)
for (ktr1=1;ktr1<=nhDataBean.statusCodeList.GetUpperBound(0);ktr1++)

How can I change the "nhDataBean.xxx.GetUpperBound(0)" so that I can use
only one function?

The code functions and how they are called:

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

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

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

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

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

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

public DataSet GetCompanyHRInfo()
{
GetProjects(ds.Tables.Add("Projects"));
GetShifts(ds.Tables.Add("Shifts"));
GetStatus(ds.Tables.Add("Status"));
return ds;
}
***************************************************************

Thanks,

Tom
 
M

Mythran

tshad said:
I have about 10 functions that are identical except for one variable name
that I am using in my "for" loop:

for (ktr1=1;ktr1<=nhDataBean.projectCodeList.GetUpperBound(0);ktr1++)
for (ktr1=1;ktr1<=nhDataBean.shiftCodeList.GetUpperBound(0);ktr1++)
for (ktr1=1;ktr1<=nhDataBean.statusCodeList.GetUpperBound(0);ktr1++)

How can I change the "nhDataBean.xxx.GetUpperBound(0)" so that I can use
only one function?

The code functions and how they are called:

Easy I hope :)

public void GetWhateverYouWantThisNamed(DataTable dt, object[] items)
{
...
for (...;ktr1 <= items.GetUpperBound(0); ktr1++)
...
}

It would be better if we knew the type for projectCodeList, shiftCodeList,
and statusCodeList. If they are all the same array type, you could use the
object type instead of "object[]"...

HTH :)

Mythran
 
T

tshad

Mythran said:
tshad said:
I have about 10 functions that are identical except for one variable name
that I am using in my "for" loop:

for (ktr1=1;ktr1<=nhDataBean.projectCodeList.GetUpperBound(0);ktr1++)
for (ktr1=1;ktr1<=nhDataBean.shiftCodeList.GetUpperBound(0);ktr1++)
for (ktr1=1;ktr1<=nhDataBean.statusCodeList.GetUpperBound(0);ktr1++)

How can I change the "nhDataBean.xxx.GetUpperBound(0)" so that I can use
only one function?

The code functions and how they are called:

Easy I hope :)

public void GetWhateverYouWantThisNamed(DataTable dt, object[] items)
{
...
for (...;ktr1 <= items.GetUpperBound(0); ktr1++)
...
}

It would be better if we knew the type for projectCodeList, shiftCodeList,
and statusCodeList. If they are all the same array type, you could use
the object type instead of "object[]"...

The objects are actually string[][] objects.

Setting it up your way (but using string instead of objects as you
suggested), looks like this:

***************************************************************
public void GetHomeStateStatus2(DataTable dt, string[][] items)
{
int ktr1 = 0;
DataRow dr;
// Add the columns to the DataTable's Columns collection
dt.Columns.Add("code");
dt.Columns.Add("description");

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

This is called like so:

GetHomeStateStatus2(ds.Tables.Add("HomeStateStatus"),etDataBean.homeStateStatus);I am getting the error:[NullReferenceException: Object reference not set to an instance of anobject.] MyFunctions.NewHire.GetWorkStateStatus2(DataTable dt, String[][] items) MyFunctions.NewHire.AddNewHireFW()I know the data is there, it shows in the debugger as:+ homeStateStatus {Length=6} string[][]If I replace the items in the functions with etDataBean.homeStateStatus, itworks fine.Thanks,Tom>> HTH :)>> Mythran
 
T

tshad

Never mind.

I got it recompiled and it started to work. I must not have copied the DLL
correctly.

So the string[][] worked fine.

Thanks,

Tom
tshad said:
Mythran said:
tshad said:
I have about 10 functions that are identical except for one variable name
that I am using in my "for" loop:

for (ktr1=1;ktr1<=nhDataBean.projectCodeList.GetUpperBound(0);ktr1++)
for (ktr1=1;ktr1<=nhDataBean.shiftCodeList.GetUpperBound(0);ktr1++)
for (ktr1=1;ktr1<=nhDataBean.statusCodeList.GetUpperBound(0);ktr1++)

How can I change the "nhDataBean.xxx.GetUpperBound(0)" so that I can use
only one function?

The code functions and how they are called:

Easy I hope :)

public void GetWhateverYouWantThisNamed(DataTable dt, object[] items)
{
...
for (...;ktr1 <= items.GetUpperBound(0); ktr1++)
...
}

It would be better if we knew the type for projectCodeList,
shiftCodeList, and statusCodeList. If they are all the same array type,
you could use the object type instead of "object[]"...

The objects are actually string[][] objects.

Setting it up your way (but using string instead of objects as you
suggested), looks like this:

***************************************************************
public void GetHomeStateStatus2(DataTable dt, string[][] items)
{
int ktr1 = 0;
DataRow dr;
// Add the columns to the DataTable's Columns collection
dt.Columns.Add("code");
dt.Columns.Add("description");

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

This is called like so:

GetHomeStateStatus2(ds.Tables.Add("HomeStateStatus"),etDataBean.homeStateStatus);I
am getting the error:[NullReferenceException: Object reference not set to
an instance of anobject.]
MyFunctions.NewHire.GetWorkStateStatus2(DataTable dt, String[][] items)
MyFunctions.NewHire.AddNewHireFW()I know the data is there, it shows in
the debugger as:+ homeStateStatus {Length=6} string[][]If I replace the
items in the functions with etDataBean.homeStateStatus, itworks
fine.Thanks,Tom>> HTH :)>> Mythran
 

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