Conserving 'const'ness

  • Thread starter Thread starter dotNEWBIE
  • Start date Start date
D

dotNEWBIE

Hi all,

I have the following sub in one of my classes that requires an optional
parameter in the form of an ole integer color. The build error I get is
'Constant expression required', so basically (I think) I need to get a
Constant value from the Color structure.

Public Class myClass

Public Sub mySub(byRef myParam as String, Optional ByRef lColor as
Integer = ColorTranslator.ToOle(SystemColors.Control))
'My code here
End Sub

End Class

Anyone able to give a few hints?

Thanks in advance.


Nick
 
Yes, but the bug is how do I get a const value from the 'Control' color to
supply to the ColorTranslator.ToOle method.
 
Pipo said:
The ColorTranslator.ToOle(SystemColors.Control) returns a constant value.

No, it actually doesn't. The property's value reflects the current system
color settings and thus is not constant (known at compile time).
 
dotNEWBIE said:
Thanks Herfried, could you suggest a possible solution to my problem?

You simply cannot use a non-constant value as default value for an optional
parameter. The workaround would be to use another constant value as default
value.
 
Maybe something like this?

Public Class myClass

Public Sub mySub(byRef myParam as String, Optional Byval
blnUseConstColor as boolean)
'My code here
if blnUseConstColor then
dim lColor as Integer =
ColorTranslator.ToOle(SystemColors.Control)
end if
End Sub

End Class
 
You might try using an overload instead of an optional parameter. This
will give the same effect but doesn't require the const expression in
the signature

e.g

Private Overloads Sub MySub( _
ByVal str As String _
)
MySub(str,
System.Drawing.Color.FromKnownColor(System.Drawing.KnownColor.Control))
End Sub

Private Overloads Sub MySub( _
ByVal str As String, _
ByVal col As Color _
)
' place code here
End Sub


hth,
Alan.
 

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

Back
Top