173901
173901

Reputation: 709

calling a method with multiple default values

i was wondering, because i have a method with multiple default parameters

private string reqLabel(string label, byte fontSize = 10, string fontColour = "#000000", string fontFamily = "Verdana"  )
{
   return "<br /><strong><span style=\"font-family: " + fontFamily + ",sans-serif; font-size:" +fontSize.ToString() + "px; color:"+ fontColour + "; \">" + label +" : </span></strong>";
}

and when i call the method it i have to do it in order

reqLabel("prerequitie(s)")
reqLabel("prerequitie(s)", 12) 
reqLabel("prerequitie(s)", 12 , "blue")
reqLabel("prerequitie(s)", 12 , "blue", "Tahoma")

so my question is, is there any way to skip the first few default parameters?

Let's say i want to input only the colour, and the font-family like this:

reqLabel("Prerequisite(s)" , "blue" , "Tahoma") 

/* or the same with 2 comma's where the size param is supposed to be. */

reqLabel("Prerequisite(s)" ,  , "blue" , "Tahoma") 

Upvotes: 6

Views: 1495

Answers (3)

PakKkO
PakKkO

Reputation: 154

You need to call with name parameters:

reqLabel("Prerequisite(s)" , fontColour: "blue" , fontFamily: "Tahoma") 

Upvotes: 2

Alberto
Alberto

Reputation: 15941

Use named arguments

reqLabel("prerequitie(s)", fontSize: 11)

Upvotes: 4

Andrei
Andrei

Reputation: 56688

Yes, it is possible with explicit naming:

reqLabel("Prerequisite(s)" , fontColour: "blue", fontFamily: "Tahoma")

Just note that named arguments should always be the last ones - you cannot specify positioned arguments after named. In other words, this is not allowed:

reqLabel("Prerequisite(s)" , fontColour: "blue", "Tahoma")

Upvotes: 11

Related Questions