col3 As Color = Nil

I’d like to create a method like this, where only two color parameters are necessary, and the third one voluntary:

Sub myMehod (col1 as color, col2 as color, col3 as color = Nil) if col3 = Nil Then ... end if ... End Sub
But a color variable can not be Nil, so I’ve found two possibilities:

1: Use a Variant instead of color
Sub myMehod (col1 as color, col2 as color, col3 as Variant = Nil)
It works, but col3 is not a color, and I don’t know if I will have problems later on, when using this variable.

2: Use a very improbable color like &c01020304

Sub myMehod (col1 as color, col2 as color, col3 as color = &c01020304) if col3 = &c01020304Then ... end if ... End Sub
Both solutions work.

Is there something better?
Which on would you use?

Use overloading, i.e. two methods

[code]Sub myMehod (col1 as color, col2 as color, col3 as color)

Sub myMehod (col1 as color, col2 as color)
[/code]

If they share code have them call a third hidden method where an extra flag can be passed

[code]Sub myMehod (col1 as color, col2 as color, col3 as color)
implMyMethod(col1, col2, col3, True)
End Sub

Sub myMehod (col1 as color, col2 as color)
implMyMethod(col1, col2, &c000000, False)
End Sub

Protected Sub implMyMehod (col1 as color, col2 as color, col3 As Color, useCol3 As boolean)
if useCol3 Then

end if

End Sub
[/code]

Thanks Will,

I’ve thought of a third possibility: Using an intermediate variable col3v. So I have a color Col3 variable.

Sub myMehod (col1 as color, col2 as color, col3v as variant= Nil) if col3v = Nil Then ... else Dim Col3 As color = col3v.ColorValue end if ... End Sub

Sub MyMethod(ParamArray cols As Color) If UBound(cols) < 1 Or UBound(cols) > 2 Then // not the proper count of colors Return // or raise an error (though runtime only!) End ... End Sub

You could use the optional keyword

Sub myMethod (col1 as color, col2 as color, optional col3 as color)_

Potential deal breaker: If col3 is not provided it will default to &c00000000

Thanks Eli and Peter

Eli: In fact my method is more complicate (different type of parameters). I made the example very easy just for the forum.
Peter: That is the problem. Black color (&c00000000) is used very often as a parameter, so I don’t want it as default.

Overload the method.

just to add details:

Colors are actually UInt32 and passed by value, so they can’t be nil.
Or use as default value a special color which is invisible.

Christian,

What do you mean by an invisible color?

Well, if you set alpha channel to transparent, the other color components don’t have a meaning.

In ChartDirector plugin we use colors with FF in alpha channel for special meanings, e.g. for gradient colors.