Solved

Test if a String can Convert to a system.media.color

Posted on 2016-07-31
10
30 Views
Last Modified: 2016-08-22
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
0
Comment
Question by:p-plater
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
what kind of string do you have here?

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.
0
 

Author Comment

by:p-plater
Comment Utility
I am using the Following to Convert the String.
supplierToPurchase.CustomerRatingBrush = TryCast(New BrushConverter().ConvertFromString(drd.GetString(10)), SolidColorBrush)

Open in new window


I am using a Try Catch but I'm after a Check if the sting will convert WITHOUT using Try Catch.
0
 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
Comment Utility
0
 

Author Comment

by:p-plater
Comment Utility
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..
0
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
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

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.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 30

Expert Comment

by:hnasr
Comment Utility
Quick reply, not checked.
WITHOUT using Try...Catch
Start 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.
0
 

Author Comment

by:p-plater
Comment Utility
I tried to use TryCast but it doesn't allow itself to be used with type "Color"

TryCast(New ColorConverter().ConvertFromString(drd.GetString(10)), Color)

Error
"TryCast Operand must be a Reference type - Color is a Value Type"
0
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
you may try:
TryCast(new BrushConverter().ConvertFromString(drd.GetString(10)), SolidBrush)

Open in new window

or
TryCast(new BrushConverter().ConvertFromString(drd.GetString(10)), SolidColorBrush)

Open in new window

0
 
LVL 11

Accepted Solution

by:
louisfr earned 500 total points (awarded by participants)
Comment Utility
BrushConverter is to get a Brush, not a Color.
Did you try ColorConverter instead?

ColorConverter.ConvertFromString will always return a Color if the string starts with # followed by 3, 4, 6 or 8 hexadecimal digits. You can use a regex or a loop to check that.

Here is the regex:
bool isOk = Regex.IsMatch(valueToParse, "^#([0-9a-fA-F]{3,4}|([0-9a-fA-F]{2}){3,4})$");

Open in new window

0
 
LVL 11

Expert Comment

by:louisfr
Comment Utility
The question wasn't about converting but checking if the conversion is possible.
The destination type wasn't a Brush but a Color.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now