Option Strict - Disallowed implicit conversion [Resolved]
I am using Option Explicit and Option Strict in my project and I have come accross an error where an implicit
conversion is taking place.
I have a LinearGradientMode and a String array. The LGM is being set from the srting array lie so. How do I convert
it so it wont error?
VB Code:
'drawParts() is the string array.
If Not IsNothing(drawParts(0)) Then _GradientPanel.DrawMode = [color=red]drawParts(0)[/color]
Re: Option Strict - Disallowed implicit conversion
drawParts(0) contains the actual constant name? like "horizontal" or whatever?
Re: Option Strict - Disallowed implicit conversion
VB Code:
GradientPanel.DrawMode = CType(drawParts(0), System.Drawing.Drawing2D.LinearGradientMode)
Re: Option Strict - Disallowed implicit conversion
Quote:
Originally Posted by ntg
VB Code:
GradientPanel.DrawMode = CType(drawParts(0), System.Drawing.Drawing2D.LinearGradientMode)
will that work if the string contains the contant name and not the actual value of the enum?
Re: Option Strict - Disallowed implicit conversion
Nope. Neither would GradientPanel.DrawMode = drawParts(0). You would need the value of the constant in the string, otherwise you'd need to fiddle with .ToString().
Re: Option Strict - Disallowed implicit conversion
That should work, but I cant test it until I get all the conversion errors fixed.
This is the other one:
VB Code:
If Not IsNothing(drawParts(1)) Then _GradientPanel.PanelColor1 = CType(drawParts(1), System.Drawing.Color)
Says string can not be converted to a color. :(
Re: Option Strict - Disallowed implicit conversion
i was thinking you could do something like this.. not sure if its overkill.. im not even sure if it works since i dont have anything to test it against....
VB Code:
For Each EnumName As String In [Enum].GetNames(GetType(LinearGradientMode))
If EnumName = drawParts(0) Then
_GradientPanel.DrawMode = [Enum].GetValues(GetType(LinearGradientMode)).GetValue(0)
End If
Next
Re: Option Strict - Disallowed implicit conversion
That shouldn't have something to do with Option Strict, right ? That's because System.Drawing.Color isn't an enumeration but a class with shared members. You shouldn't be able to do this using Option Explicit either.
I would think that should read:
VB Code:
If EnumName.ToString() = drawParts(0) Then
right ?
Re: Option Strict - Disallowed implicit conversion
well in my example enumname already is a string...
[Enum].GetNames(GetType(LinearGradientMode))
returns a string array
Re: Option Strict - Disallowed implicit conversion
Re: Option Strict - Disallowed implicit conversion
So then how do I fix the one for the color?
Re: Option Strict - Disallowed implicit conversion
VB Code:
color.fromname(drawParts(1))
i think should do it
Re: Option Strict - Disallowed implicit conversion
Havent ran it yet but no error lines :D
Will this cover if the color is an RGB?
Re: Option Strict - Disallowed implicit conversion
Quote:
Originally Posted by RobDog888
Havent ran it yet but no error lines :D
Will this cover if the color is an RGB?
ummmmmmm NO, it takes a color name as a param..
there is color.FromArgb() which you can pass an integer color value, or pass a red, green, and blue value to (its overloaded).. so i guess it depends on how your string looks...
like if your string is "255,255,255" you might need to write a little function that parses and returns an array of integers to pass to the color.FromArgb() function
Re: Option Strict - Disallowed implicit conversion
Damn, the thing is that its a property. So the user could enter a RGB or select a known color.
Got this error when I built the control:
Code generation for property 'Controls' failed. Error was: ''GraphicsModeConverter' is unable to convert 'MyUC.GradientPanelControl.GradientPanel' to 'System.ComponentModel.Design.Serialization.InstanceDescriptor'.'
Re: Option Strict - Disallowed implicit conversion
Ok, I rebuilt both the control's project and the test project and the error went away. I tested under an known
named color and an RGB and it appears to work without errors but my properties are not expandable
properties.
Thanks for the help ntg and kleinma :thumb:
Please see Expandable Properties thread for the other part of this issue.
Re: Option Strict - Disallowed implicit conversion [Resolved]
You can use the Parse method of the Enum type to convert a constant name into it's appropriate value, i.e.
VB Code:
_GradientPanel.DrawMode = [Enum].Parse(GetType(LinearGradientMode), drawParts(0))
If Not IsNumeric(drawParts(1)) Then
_GradientPanel.PanelColor1 = [Enum].Parse(GetType(KnownColor), drawParts(1))
Else
_GradientPanel.PanelColor1 = Color.FromArgb(Convert.ToInt32(drawParts(1)))
End If
Re: Option Strict - Disallowed implicit conversion [Resolved]
Cool! :thumb:
You always make things look so easy!