Is there a template for passing a parameter to another assembly?

From several of my Assemblies such as AssemblyA, AssemblyB, AssemblyC, AssemblyD I call a method in AssemblyE

eg:

DialogHelper.DisplayComments( param1,2,3,4) ;

      

now for some of these assemblies, and not all of them, I need to have an overloaded version of the DisplayComments () method that takes another new parameter that I want to pass:

DialogHelper.DisplayComments(param1,2,3,4,"5") ;

      

So one way is what I did: create an overload and pass the value.

But I was wondering if .NET and the Obeject-Oriented world have any other elegant way to do this to pass this value to this Assembly and this method?

+3


source to share


4 answers


The OOP

overload of the world is already very graceful.

If you want to try something else, you can add More Options like



DialogHelper.DisplayComments(string param1, int prm2, int prm3, int prm4, string paramOtional="5") ;

      

Good luck.

+1


source


If you have the source code, it is better to overload the method DisplayComments



otherwise (if they are not static methods) you can create an extension method that takes another parameter

+2


source


You can use the params keyword . Since you have mixed types (int and string), you have two options:

Use object []

public void DisplayComments(params object[] myVariableLengthParams)

      

Use a string parameter after the first 4 parameters:

public void DisplayComments(int param1, int param2, int param3, 
    int param4, params string[] myVariableStringParams)

      

OR, you can use the defaults if you are using 4.0 (this may not be the best option if you are using the public API. It may leave things out of sync with expectations if the default changes with respect to Daniel Hilgart's comment below.)

public void DisplayComments(int param1, int param2, int param3, 
    int param4, string param5 = "")

      

Finally, you can use overloads (however it seems like an option params

might be better for you). And this is almost the same as using the default values if the logic is the same as for

 public void DisplayComments(int param1, int param2, int param3, int param4)
 {
     DisplayComments(int param1, int param2, int param3, int param4, "");
 }

 public void DisplayComments(int param1, int param2, int param3, int param4, 
     string param5)
 {
     //Final overload that does the logic
 }

      

if the logic is not the same

 public void DisplayComments(int param1, int param2, int param3, int param4)
 {
     //Logic for 4 param
 }

 public void DisplayComments(int param1, int param2, int param3, int param4, 
     string param5)
 {
     //Logic for 5 param
 }

      

You are allowed to have many methods with the same name if the method signatures are not the same (same order, same types, same return type)

+1


source


Try not to consider passing data to another assembly. Instead, focus on simply passing data between objects and methods. Which assembly of these types does not matter. The way to think about DialogHelper

is as a service. This provides a dialogue for consumers who have separate needs. Where they live, it doesn't matter what they want, this is the important part.

There seems to be an additional comment parameter here that some consumers need. It's perfectly reasonable to think of it as an overloaded function or as a single function with an optional parameter.

public class DialogHelper {
  public static void DisplayComments(
    object param1, 
    int param2,  
    int param3,
    int param4,
    string param5 = "") {
    ...
  }
}

      

+1


source







All Articles