patterncsharpModerate
Writing a reusable method in which only some of its parameters are different
Viewed 0 times
methodarewritingdifferentreusableitssomewhichparametersonly
Problem
I have the method below but I am going to have more methods similar to this one. They differ in is the last parameters being passed to the method. The first three parameters will always be there and then in the
I am going to need five or six of these methods. Do you think I can write this method in a more reusable way, or do I have to just copy/paste most of it and change the params and condition for each method?
while loop with && conditions, I am going to use the same params that I am passing in.I am going to need five or six of these methods. Do you think I can write this method in a more reusable way, or do I have to just copy/paste most of it and change the params and condition for each method?
private string FindFullRowForProvider(string id, string lastName, string firstName, string devSystem)
{
string fullRow = string.Empty;
StreamReader streamreader = new StreamReader(importSource);
char[] delimiter = new char[] { '|' };
int columnLocation = 0;
string[] columnheaders = streamreader.ReadLine().Split(delimiter);
foreach (string columnheader in columnheaders)
{
columnHeadertoLocation[columnheader.ToUpper()] = columnLocation;
columnLocation++;
}
while (streamreader.Peek() > 0)
{
fullRow = streamreader.ReadLine();
string[] currentRowValues = fullRow.Split(delimiter);
string _NPI = currentRowValues[columnHeadertoLocation["ID"]];
string _LName = currentRowValues[columnHeadertoLocation["LNAME"]];
string _FName = currentRowValues[columnHeadertoLocation["FNAME"]];
string _devPKCR = currentRowValues[columnHeadertoLocation["DEV_SYSTEM"]];
if (npi.Trim().ToUpper() == _NPI.Trim().ToUpper()
&& _LName.Trim().ToUpper() == lastName.Trim().ToUpper()
&& _FName.Trim().ToUpper() == firstName.Trim().ToUpper()
&& _dev.Trim().ToUpper() == devSystem.Trim().ToUpper())
{
// woot! we found the row of this guy.
return fullRow;
}
}
return fullRow;
}Solution
What you're looking for is an Optional Argument. When using optional arguments, you have to supply a default value. So, you just have to check for that default value, and switch your control flow appropriately. It would look something like this. (Note I used empty quotes because
But... this is Code Review after all, so let's take a look at what you've done.
I'm making an assumption that
I like your use of
Maybe I'm missing something, but I think declaring a character array for delimiter is overkill here.
I'm not very good at C#, so there may be an even simpler way, but you could certainly just split it like this.
Nice use of brackets and formatting here.
String.Empty isn't a constant expression.)private string FindFullRowForProvider(string id, string lastName, string firstName, string devSystem = "")
{
//maybe some code here
if (devSystem == String.Empty)
{
//handle that case
}
//maybe some more code here
}But... this is Code Review after all, so let's take a look at what you've done.
private string FindFullRowForProvider(string id, string lastName, string firstName, string devSystem)I'm making an assumption that
id is tied to lastName and firstName, but it looks like you're missing a class in your design. Creating a Person class would simplify the signature an awful lot. Be wary of passing lots of strings to a method. private string FindFullRowForProvider(Person somePerson, string devSystem = "")I like your use of
String.Empty. I really do prefer it over empty quotes. I think it makes for nice readable code. Well done.string fullRow = string.Empty;Maybe I'm missing something, but I think declaring a character array for delimiter is overkill here.
char[] delimiter = new char[] { '|' };
int columnLocation = 0;
string[] columnheaders = streamreader.ReadLine().Split(delimiter);I'm not very good at C#, so there may be an even simpler way, but you could certainly just split it like this.
string[] columnheaders = streamreader.ReadLine().Split(new char[] { '|' });Nice use of brackets and formatting here.
if (npi.Trim().ToUpper() == _NPI.Trim().ToUpper()
&& _LName.Trim().ToUpper() == lastName.Trim().ToUpper()
&& _FName.Trim().ToUpper() == firstName.Trim().ToUpper()
&& _dev.Trim().ToUpper() == devSystem.Trim().ToUpper())
{
// woot! we found the row of this guy.
return fullRow;
}Code Snippets
private string FindFullRowForProvider(string id, string lastName, string firstName, string devSystem = "")
{
//maybe some code here
if (devSystem == String.Empty)
{
//handle that case
}
//maybe some more code here
}private string FindFullRowForProvider(string id, string lastName, string firstName, string devSystem)private string FindFullRowForProvider(Person somePerson, string devSystem = "")string fullRow = string.Empty;char[] delimiter = new char[] { '|' };
int columnLocation = 0;
string[] columnheaders = streamreader.ReadLine().Split(delimiter);Context
StackExchange Code Review Q#67436, answer score: 11
Revisions (0)
No revisions yet.