[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2016-07-31
10
Medium Priority
?
56 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 53

Expert Comment

by:Ryan Chong
ID: 41736899
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
ID: 41736933
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 83

Expert Comment

by:David Johnson, CD, MVP
ID: 41736946
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

Author Comment

by:p-plater
ID: 41736960
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 53

Expert Comment

by:Ryan Chong
ID: 41736991
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
 
LVL 31

Expert Comment

by:hnasr
ID: 41736995
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
ID: 41737022
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 53

Expert Comment

by:Ryan Chong
ID: 41737025
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 2000 total points (awarded by participants)
ID: 41737050
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
ID: 41763499
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

656 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