Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method parameter to accept multiple types

I'm developing an application which in I got multiple types of RichTextBoxs which I've customized (RichTextBox,RichAlexBox,TransparentRichTextBox).

I want to create a method to accept all those types plus some other parameters.

private void ChangeFontStyle(RichTextBox,RichAlexBox,TransparentRichTextBox rch,
                                                         FontStyle style, bool add)
{
  //Doing somthing with rch.Rtf
}

I've searched through StackOverFlow and found some answers like this which I couldn't figure out how to use them to solve my problem

void foo<TOne, TTwo>()  //There's just one parameter here
   where TOne : BaseOne //and I can't figure out how to define my other two parameters        
   where TTwo : BaseTwo

I also tried overloading as this answer offers,

private void ChangeFontStyle(TransparentRichTextBox rch, FontStyle style, bool add);
private void ChangeFontStyle(RichAlexBox rch, FontStyle style, bool add);
private void ChangeFontStyle(RichTextBox rch,FontStyle style, bool add)
  {
    //Some codes
  }

But I encounter this error

'ChangeFontStyle(RichAlexBox, FontStyle, bool)' 
         must declare a body because it is not marked abstract, extern, or partial

Here's some other Q&A I checked:

Generic method with multiple constraints

Can I create a generic method that accepts two different types in C#

C# generic method with one parameter of multiple types

Any suggestion would be appreciated.

like image 962
Alex Jolig Avatar asked Oct 26 '15 06:10

Alex Jolig


People also ask

How to pass multiple Parameters in java method?

Parameters are specified after the method name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma.

How many types of parameters method can have?

Out Parameters. Default Parameters or Optional Arguments (C# 4.0 and above) Dynamic parameter (dynamic keyword). Value parameter or Passing Value Types by Value (normal C# method param are value parameter)

What is a Parameter in a method?

Note: Parameters refers to the list of variables in a method declaration. Arguments are the actual values that are passed in when the method is invoked. When you invoke a method, the arguments used must match the declaration's parameters in type and order.

What is a method parameter modifier?

ref (reference) parameter: If a parameter is attached with a ref modifier, then changes will be made in a method that affect the calling method. This is also known as call-by-reference.


3 Answers

Assuming that TransparentRichTextBox and RichAlexBox are both subtypes of RichTextBox, then you only need one method signature:

private void ChangeFontStyle(RichTextBox rch, FontStyle style, bool add)
{
    //Doing somthing with rch.Rtf
}

Otherwise, you will need to implement the method for every overload:

private void ChangeFontStyle(TransparentRichTextBox rch, FontStyle style, bool add)
{
    //Some codes
}

private void ChangeFontStyle(RichAlexBox rch, FontStyle style, bool add)
{
    //Some codes
}

private void ChangeFontStyle(RichTextBox rch,FontStyle style, bool add)
{
    //Some codes
}
like image 184
poke Avatar answered Oct 02 '22 20:10

poke


If you are properly using inheritance, you don't need generics or overloading:

class RichTextBox
{
     // implementation
}

class RichAlexBox : RichTextBox
{
     // implementation
}

class TransparentRichTextBox : RichTextBox
{
     // implementation
}

// allows passing in anything that inherits from RichTextBox
private void ChangeFontStyle(RichTextBox rch, FontStyle style, bool add)
{
     // implementation
}

As @dotnetkid suggests, another option is to make ChangeFontStyle a method in the RichTextBox class and make it virtual so that you can override it when you need to:

class RichTextBox
{
     public virtual void ChangeFontStyle(FontStyle style, bool add)
     {
         // implementation
     }

     // implementation
}

class RichAlexBox : RichTextBox
{
     // uses the inherited ChangeFontStyle

     // implementation
}

class TransparentRichTextBox : RichTextBox
{
     public override void ChangeFontStyle(FontStyle style, bool add)
     {
         // TransparentRichTextBox-specific implementation
     }

     // implementation
}
like image 24
JLRishe Avatar answered Sep 29 '22 20:09

JLRishe


When overloading a method do like this.

private void ChangeFontStyle(TransparentRichTextBox rch, FontStyle style, bool add)
{
   // your code
}

private void ChangeFontStyle(RichAlexBox rch, FontStyle style, bool add) 
{
   // your code
}
private void ChangeFontStyle(RichTextBox rch,FontStyle style, bool add)
{
   // your code
}

Or you can just use Control class as a parameter try like this

 private void ChangeFontStyle(Control control,FontStyle style, bool add)
 {
       // your code
 }

You can pass any Control you want like TextBox, RichTextBox, ComboBox etc..

like image 43
dan Avatar answered Oct 01 '22 20:10

dan