Nidrax
Nidrax

Reputation: 370

C# Using any type of object as function parameter

I want to make an animator class in C# that will perform some effects on objects (like sliding them right/left, changing their opacity, etc.) And I wonder if it is possible to call a function with any type of object as a parameter instead of having overloaded functions: one for labels, another for pictureBoxes and another one for panels.

Upvotes: 2

Views: 151

Answers (3)

clarkitect
clarkitect

Reputation: 1730

You have two options:

  1. You can use the generic type MyFunction<T>()
  2. You can use object and figure out casting within the method.

I'd recommend the first option. Lots more info about it here on stack overflow and also on the MSDN

Upvotes: 0

Mat&#237;as Fidemraizer
Mat&#237;as Fidemraizer

Reputation: 64943

Your animator method will need to accept Control instances, where Control is the base class of any control.

Upvotes: 2

Stilgar
Stilgar

Reputation: 23591

Yes. Use a common base type. In your case this is probably Control

Upvotes: 4

Related Questions