Why doesn't this statement work?

  • Thread starter Thread starter Jim
  • Start date Start date
J

Jim

Const MyDefaultColor As System.Drawing.Color =
System.Drawing.Color.FromArgb(CType(CType(52, Byte), Integer),
CType(CType(102, Byte), Integer), CType(CType(151, Byte), Integer))
 
Const MyDefaultColor As System.Drawing.Color =
System.Drawing.Color.FromArgb(CType(CType(52, Byte), Integer),
CType(CType(102, Byte), Integer), CType(CType(151, Byte), Integer))

My VB says: "constant expression required", thus the answer is: you need a
constant expression. ColorFromArgb is a function executed at runtime, not a
constant expression.

Alternative:
Public Shared MyDefaultColor As System.Drawing.Color = _
System.Drawing.Color.FromArgb( _
CType(CType(52, Byte), Integer), _
CType(CType(102, Byte), Integer), _
CType(CType(151, Byte), Integer) _
)

But to make it simpler:
CType(52, Byte) -> CByte(52)
CType(CByte(52), Integer) -> CInt(CByte(52)) -> CInt(52)


->
Public Shared MyDefaultColor As System.Drawing.Color = _
System.Drawing.Color.FromArgb(52, 102, 151)


Armin
 
Const MyDefaultColor As System.Drawing.Color =
System.Drawing.Color.FromArgb(CType(CType(52, Byte), Integer),
CType(CType(102, Byte), Integer), CType(CType(151, Byte), Integer))

Because "Constants must be of an intrinsic or enumerated type, not a class,
structure, type parameter, or array type." and System.Drawing.Color is a
structure.

But you don't need a constant. A shared readonly variable has the same
effect

Public Shared ReadOnly MyDefaultColor As System.Drawing.Color =
System.Drawing.Color.FromArgb(52, 102, 151)

David
 
why are you trying to convery an interger to a byte and right back to an
interger again? seems pointless... just use the integer
 
Back
Top