Visual C# Developer Center >
Visual C# Forums
>
Visual C# General
>
Cannot use a 0xHEX int value from a parsed string
Cannot use a 0xHEX int value from a parsed string
- I've got a problem..
I want to have some 0xHEX value, with is user defined.
The HEX code is a Color object with is ColorTranslated to a Html code (Hex).
Before I use it, I will save the Html output into a string, so I can use it later (Have to be a string for the server).
Thats Html thing, does not contain the 0x part..
I tried:
--- c[2] is the HEX string value --- 1: int.Parse("0x"+c[2]); 2: Convert.ToInt32("0x"+c[2]); 3: Convert.ToInt32("0x"+c[2], 16); 4: Convert.ToInt32(c[2]); 5: Convert.ToInt32(c[2], 16); Ouch, giveup.
Note: I will HAVE to use 0xHEXHEX becouse that's the only supported method.
How can I fix this?
Answers
- int value = ColorTranslator.FromHtml(htmlString).ToArgb();to convert back:string html = ColorTranslator.ToHtml(Color.FromArgb(value));If you're going to put it on HTML, it makes no difference whether it shows up as "#FF0000" or "Red". The browser will know the color by name and translate accordingly.Do me a favor before you post anything else.Look up System.Drawing.Color on MSDN. Go to the "members" link on the left, that will show you all of the members of color. Read all the summaries of the methods.Next, do the same for System.Drawing.ColorTranslator.This is the kind of problem that comes up all the time in programming. Your job is to figure out how to put those methods together and get the values in the format you need. It's all there for you, but you need to learn to hunt it down. I bet you can. :)
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser- Marked As Answer by_Yoshi_ Friday, November 06, 2009 1:00 PM
All Replies
- Int32.Parse(c[2], NumberStyles.HexNumber);
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser- Edited byDavid M MortonMVP, ModeratorTuesday, November 03, 2009 7:35 PM
- Error 1 The name 'NumberStyles' does not exist in the current context ... 337 89 ...
What's wrong... - You need "using System.Globalization;" at the top of your code.
Put your cursor in the word "NumberStyles". Press Ctrl+. (ctrl+<period>). Press enter.
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser - The last step won't do anything..
And I've got 3 errors instead of one now.. I think it's about the last step :). - Those weren't steps. They were two methods to get at the same thing.
Post your code and the errors you're getting.
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser - It sounds like you manually added "using System.Globalization", then put the cursor on "NumberStyles", pressed Ctrl+. and pressed Enter.
Doing that (in that order) would result in a line-break in the middle of the identifier "NumberStyles", which would indeed produce errors.
It was a one or the other thing.
The full-name of NumberStyles is "System.Globalization.NumberStyles". This is quite onerous to type, so we add:
using System.Globalization;
to the top of the source file. Now when the compiler sees "NumberStyles" (an incomplete type name), it looks at all the using statements at the top of the file and tries pre-pending each of them to see if one will result in a complete type name (that's a bit of a simplification).
That's the purpose of the 'using' statement. It allows us to abbreviate type names. Most C# file have:
using System;
at the top. That's because a great number of the types we use are actually abbreviations for System.Something.
Anyhow, there are two ways to resolve incomplete type names (where you know the type is in a referenced assembly, but didn't specify the full type name and the compiler complains that 'SuchAndSuch' does not exist in the current context):
1) Add a using statement at the top by typing it. This requires that you know the full type name to begin with.
2) Move the cursor to the abbreviated name and notice that a red underline appears under the last letter. That underline is actually a little widget you can hover the mouse over. Doing so gives you a drop-down button. Clicking on the button opens a menu of choices. In the simplest case there will be two options. I'll use the NumberStyles example here:
"using System.Globalization;"
"System.Globalization.NumberStyles"
Selecting the first option will add a using statement to the top of the file. Now you can abbreviate all the types in that namespace.
Selecting the second option will change the text "NumberStyles" to "System.Globalization.NumberStyles", giving the full type name in that location only.
David told you to press Ctrl+. This is a keyboard shortcut that allows you to invoke the drop-down without using the mouse. Very handy when you have both hands on the keyboard and are typing fast. - As I see now, the using will work fine, because it finds it.
But it will NOT work in combination with the Convert.ToInt32... As I see now, the using will work fine, because it finds it.
But it will NOT work in combination with the Convert.ToInt32...
Sorry, I apologize profusely. It should have been
Int32.Parse(c[2], NumberStyles.HexNumber);
Not
Convert.ToInt32(c[2], NumberStyles.HexNumber);
My fault.
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser- Aha :P.
I will try tomorrow when I'm back on :). - I missed that too. I even wrote a little app (well, added a line of code to something I was working on) to see what happens if you press Ctrl+. and Enter when the type name is already resolved. However, I subconsciously used int.Parse rather than Convert.ToInt32.
- Fine:
System.FormatException was unhandled
Message="De indeling van de invoertekenreeks is onjuist."
Source="mscorlib"
StackTrace:
......................
I use: Int32.Parse(c[2], NumberStyles.HexNumber)
What now? - Use int.TryParse instead.
int value; if (int.TryParse(c[2], NumberStyles.HexNumber, CultureInfo.CurrentCulture, out value)) { // value contains a valid number } else { // input string was not a valid hexidecimal number, deal with the error }
- I don't think tryparse will work in case.
First the user is able to select a simple color from the standart mixer. That color will be 'stored' in a button's FlatStyle.BorderColor prop..
Later, the user press a button.
Then I get the translate using the ColorTranslator - the BorderColor to some 'hex' code and ToString() it.
I will try to print the value of c[2]... - Ok, this is the horrible result.
I use this code:
ColorTranslator.ToHtml(selectcolor.FlatAppearance.BorderColor).ToString()
with will result in a print of the color's name, like Red, Maroon etc.. But I want a color value :Q. - I think you're going about this all wrong. Skip the whole Int.Parse thing completely. If you want to store the value as an integer use this:
int colorValue = selectcolor.FlatAppearance.BorderColor.ToArgb();
To get it back:
Color color = Color.FromArgb(colorValue);
To convert it to an HTML representation:
string htmlColor = ColorTranslator.ToHtml(color);
You can also use the ColorTranslator methods that use ToWin32 and FromWin32.
Experiment a bit. Int32.Parse and Convert.ToInt32 should have nothing to do with this. You have more than enough methods at your disposal to avoid these.
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser - Yes, that worked also before, but now I HAVE TO convert it to a string to recover it back. I'm never able to store int's or other values in my program..
I think I still need that then.
And on some really scary way (I didn't did anything actually, but it worked after 2 times :Q), now I get printed #0A76A8 :D.
Still I get an error at this part: Int32.Parse(c[2],NumberStyles.HexNumber).. It said that the number collection is incorrect. - --- edit ---
Problem.. :(
Sometimes it will return a word, and sometimes it returns a real hex code.. What to do now? - int value = ColorTranslator.FromHtml(htmlString).ToArgb();to convert back:string html = ColorTranslator.ToHtml(Color.FromArgb(value));If you're going to put it on HTML, it makes no difference whether it shows up as "#FF0000" or "Red". The browser will know the color by name and translate accordingly.Do me a favor before you post anything else.Look up System.Drawing.Color on MSDN. Go to the "members" link on the left, that will show you all of the members of color. Read all the summaries of the methods.Next, do the same for System.Drawing.ColorTranslator.This is the kind of problem that comes up all the time in programming. Your job is to figure out how to put those methods together and get the values in the format you need. It's all there for you, but you need to learn to hunt it down. I bet you can. :)
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser- Marked As Answer by_Yoshi_ Friday, November 06, 2009 1:00 PM
- Wow..
I just think I switched Argb with Html.
With converting it to Argb it WILL work properly always :D.
The summaries are very useful, even that whole libary of stuff O_O. Thanks.
Still one problem releases that all colors are switched (red is switched with blue etc.), but I think I'm able to solve that myself. Thank you.


