Various types in params

S

Shapper

Hello,

I have the following:

public static String Check(params MyFirstClass[] Tips) {
}

Is it possible to pass parameters of two different types, e.g.,
MyFirstClass and MySecondClass?

Then inside the method I need to perform a different action depending
on each parameter type.

Thank You,
Miguel
 
J

Jeff Johnson

I have the following:

public static String Check(params MyFirstClass[] Tips) {
}

Is it possible to pass parameters of two different types, e.g.,
MyFirstClass and MySecondClass?

Then inside the method I need to perform a different action depending
on each parameter type.

Are you asking if the Tips parameter (which is declared as an array of
MyFirstClass) can hold both MyFirstClass and MySecondClass? Only if
MySecondClass derives from MyFirstClass.

If that wasn't your question, then I don't actually understand the question,
unless you're simply asking how to pass different types in a params
parameter, in which case the answer is to declare that parameter as
object[].
 
R

Registered User

Hello,

I have the following:

public static String Check(params MyFirstClass[] Tips) {
}

Is it possible to pass parameters of two different types, e.g.,
MyFirstClass and MySecondClass?

Then inside the method I need to perform a different action depending
on each parameter type.
This is not a wise idea. Use your favorite search engine to look up
"principle of single responsibility".

It probably would be better to overload the Check method and have each
method contain its own type-specific functionality.

public static String Check(params MyFirstClass[] Tips) {
}

public static String Check(params MySecondClass[] Tips) {
}

regards
A.G.
 
S

Shapper

I have the following:
public static String Check(params MyFirstClass[] Tips) {
}
Is it possible to pass parameters of two different types, e.g.,
MyFirstClass and MySecondClass?
Then inside the method I need to perform a different action depending
on each parameter type.

This is not a wise idea. Use your favorite search engine to look up
"principle of single responsibility".

It probably would be better to overload the Check method and have each
method contain its own type-specific functionality.

public static String Check(params MyFirstClass[] Tips) {

}

public static String Check(params MySecondClass[] Tips) {

}

regards
A.G.

Thank You,
Miguel
 
A

Arne Vajhøj

I have the following:

public static String Check(params MyFirstClass[] Tips) {
}

Is it possible to pass parameters of two different types, e.g.,
MyFirstClass and MySecondClass?

Then inside the method I need to perform a different action depending
on each parameter type.

If MyFirstClass and MySecondClass are concrete sub classes
of an abstract base class MyClasses then you can use:

public static String Check(params MyClasses[] Tips)

and call the methods defined but not necessarily implemented
in MyClasses.

You can do the same with a common interface.

If that is not the case then

public static String Check(params object[] Tips)

is valid C#.

But it is with 98% probability a bad idea of testing
on type of Tips[ix] inside the method.

Testing on actual type is completely anti-OO'ish.

And you will likely create a maintenance nightmare by
the need to add tests for new types all the time.

Don't do it!

Arne
 

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