473,466 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Conserving 'const'ness

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
Nov 21 '05 #1
7 1135
The ColorTranslator.ToOle(SystemColors.Control) returns a constant value.

"dotNEWBIE" <Newbie@dotNET> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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

Nov 21 '05 #2
Yes, but the bug is how do I get a const value from the 'Control' color to
supply to the ColorTranslator.ToOle method.

"Pipo" <No****@me.com> wrote in message
news:ev**************@TK2MSFTNGP09.phx.gbl...
The ColorTranslator.ToOle(SystemColors.Control) returns a constant value.

"dotNEWBIE" <Newbie@dotNET> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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


Nov 21 '05 #3
"Pipo" <No****@me.com> schrieb:
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).

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #4
Thanks Herfried, could you suggest a possible solution to my problem?

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:eq**************@TK2MSFTNGP14.phx.gbl...
"Pipo" <No****@me.com> schrieb:
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).

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #5
"dotNEWBIE" <Newbie@dotNET> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #6
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
"dotNEWBIE" <Newbie@dotNET> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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

Nov 21 '05 #7
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.

Nov 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Thomas Matthews | last post by:
Hi, Given a structure of pointers: struct Example_Struct { unsigned char * ptr_buffer; unsigned int * ptr_numbers; }; And a function that will accept the structure:
4
by: James Aguilar | last post by:
Take the following code example: class Array { double *m_array; public: Array() { m_array = new double; } double *begin() const {return m_array;} }; int main() {
8
by: Laurijssen | last post by:
What are the advantages of using const as often as possible in C? Does it help to declare integer function arguments as const? How about pointers and automatic const integers? const int...
3
by: dstevel | last post by:
The signature for strtol is: strtol( const char*, char**, int) So.. if we start with a passed "const char*" (pointer to const char), then we can't create a non-const char pointer pointer to...
16
by: hzmonte | last post by:
Correct me if I am wrong, declaring formal parameters of functions as const, if they should not be/is not changed, has 2 benefits; 1. It tells the program that calls this function that the...
9
by: Ben Voigt | last post by:
Found another variation on my previous bug (https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=100917) I would welcome validation and votes. class...
35
by: Sean Farrow | last post by:
Hi: What is best and safest way of converting a char* to a const char *? Can I use const_cast? Cheers Sean.
5
by: Rahul | last post by:
Hi, Why does the following gives a compilation error int const * * p1= 0; int * * p2 = 0; p1 = p2; p1 has more const restriction than p2 so increasing const'ness should always be...
5
by: coolguyaroundyou | last post by:
Consider the following codes: class abc { int i; public: abc() : i(0) {} void func() { .....some code....} };
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.