Ron Kidd
asked on
Test if a String can Convert to a system.media.color
Is there a way to check if a String will Convert to a system.media.color type without a Try...Catch Block and throwing an Exception?
There must be but I just can't find it...
Thanks
There must be but I just can't find it...
Thanks
ASKER
I am using the Following to Convert the String.
I am using a Try Catch but I'm after a Check if the sting will convert WITHOUT using Try Catch.
supplierToPurchase.CustomerRatingBrush = TryCast(New BrushConverter().ConvertFromString(drd.GetString(10)), SolidColorBrush)
I am using a Try Catch but I'm after a Check if the sting will convert WITHOUT using Try Catch.
list of system-defined colors. https://msdn.microsoft.com/en-us/library/windows/desktop/bb189018.aspx
ASKER
The string is like #256142.
I am Not after system colors..
I am after a way to TEST if a String will convert to a Color WITHOUT using Try...Catch..
I am Not after system colors..
I am after a way to TEST if a String will convert to a Color WITHOUT using Try...Catch..
TryCast ensures that your program will not getting an error if you passing the wrong parameter into the function ConvertFromString.
TryCast Operator (Visual Basic)
https://msdn.microsoft.com/en-us/library/zyy863x8(v=vs.100).aspx
you need to handle that as part of a good practice in programming. you can always do that without the TryCast, but your program could be at risk of crashing due to program error exception.
TryCast Operator (Visual Basic)
https://msdn.microsoft.com/en-us/library/zyy863x8(v=vs.100).aspx
If an attempted conversion fails, CType and DirectCast both throw an InvalidCastException error. This can adversely affect the performance of your application. TryCast returns Nothing (Visual Basic), so that instead of having to handle a possible exception, you need only test the returned result against Nothing.
you need to handle that as part of a good practice in programming. you can always do that without the TryCast, but your program could be at risk of crashing due to program error exception.
Quick reply, not checked.
Test the required value if exists in the table.
WITHOUT using Try...CatchStart by creating a temporary table containing all available color codes, or just the ones of interest.
Test the required value if exists in the table.
ASKER
I tried to use TryCast but it doesn't allow itself to be used with type "Color"
TryCast(New ColorConverter().ConvertFr omString(d rd.GetStri ng(10)), Color)
Error
"TryCast Operand must be a Reference type - Color is a Value Type"
TryCast(New ColorConverter().ConvertFr
Error
"TryCast Operand must be a Reference type - Color is a Value Type"
you may try:
TryCast(new BrushConverter().ConvertFromString(drd.GetString(10)), SolidBrush)
orTryCast(new BrushConverter().ConvertFromString(drd.GetString(10)), SolidColorBrush)
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
The question wasn't about converting but checking if the conversion is possible.
The destination type wasn't a Brush but a Color.
The destination type wasn't a Brush but a Color.
you probably can't transfer a String to system.media.color object directly unless there is a built-in function there, and it wil throw an exception if invalid string input parameter was found. hence a try catch would probably tell you whether your string input parameter is valid.