473,396 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Cast Object to a Color

Hi,

I'm currently working on a function which reads an xml file and automatically checks whether the information in this file may be used or not. If it finds a row which cannot be used it will automatically replace this row by a default row which is hardcoded into the program.

My problem is, that I don't want to put the color codes as strings into my DataTable but convert them to actual Colors right away. This succeeds but when I try to cast the color out of the DataTable it fails and I don't see why because I have seen this in several examples.

The program does the following:
String -> Hex -> Color -> Save into DataTable -> Cast as Color

and the last step fails. Does anyone know why and how I can fix this?

best regards
Cyclonit
Dec 7 '11 #1
5 3782
adriancs
122 100+
show the code where the error occur
Dec 7 '11 #2
I'm doing the following to write the color into the DataTable:

Expand|Select|Wrap|Line Numbers
  1. String colorHex = (String)fileRow.ItemArray[3];
  2. colorHex = colorHex.TrimStart('#');
  3.  
  4. Int32 R = Int32.Parse(colorHex.Substring(0, 2), NumberStyles.AllowHexSpecifier);
  5. Int32 G = Int32.Parse(colorHex.Substring(2, 2), NumberStyles.AllowHexSpecifier);
  6. Int32 B = Int32.Parse(colorHex.Substring(4, 2), NumberStyles.AllowHexSpecifier);
  7.  
  8. Color color = Color.FromArgb(255, R, G, B);
  9. Object colorObj = color;
  10.  
  11. tempDesign.Rows.Add((String)fileRow.ItemArray[0], (String)fileRow.ItemArray[1], (String)fileRow.ItemArray[2], colorObj);
When I check how the entry looks like in the DataTable (via "run to cursor") VisualStudio shows it like this:
Expand|Select|Wrap|Line Numbers
  1. colorObj = "{Name=ffff00aa, ARGB=(255, 255, 0, 170)}"
My code to extract it (not final, just for debugging) is:
Expand|Select|Wrap|Line Numbers
  1. Object colorObj = SMDesign.Design.Rows[0].ItemArray[3];
  2. Color color = (Color)colorObj;
And VisualStudio shows the object this way:
Expand|Select|Wrap|Line Numbers
  1. colorObj = "Color [A=255, R=255, G=0, B=170]"
Dec 7 '11 #3
adriancs
122 100+
well, I have tried storing and retrieving color value in and from a DataTable with the following code:

Expand|Select|Wrap|Line Numbers
  1. DataTable dt = new DataTable();
  2. dt.Columns.Add("color", typeof(Color));
  3. dt.Rows.Add(Color.FromArgb(255,123,132,132));
  4. dt.Rows.Add(Color.Red);
  5. dt.Rows.Add(Color.FromName("yellow"));
  6. Color a = (Color)dt.Rows[0][0];
  7. Color b = (Color)dt.Rows[1][0];
  8. Color c = (Color)dt.Rows[2][0];
Is this solve your problem?
Dec 8 '11 #4
Yes thank you very much.
Dec 8 '11 #5
adriancs
122 100+
Hi, you are welcome. perhaps, you may consider to mark "choose as best answer button" on the most appropriate post that has solved your problems. :-)
Dec 9 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Pankaj Jain | last post by:
Hi All, I have a class A which is derived from ServicesComponent to participate in automatic transaction with falg Transaction.Required. Class A is exposed to client through remoting on Http...
1
by: hrhoe | last post by:
Hi, I'm trying to create a reusable component with C#. So, I created a user control that uses Microsoft SQLDMO Object Library (SQLDMO). And I could add that UserControl on Toolbox. However,...
3
by: Imran Aziz | last post by:
Hello All, I am getting the following error on our production server, and I dont get the same error on the development box. Unable to cast object of type 'System.Byte' to type 'System.String'. ...
8
by: | last post by:
I have the following class : Public Class MyTreeNode Inherits TreeNode Dim mystring As String End Class Now, when I try to do this : ''''''''''''nodes is a TreeNodeCollection, s is string
0
by: sam | last post by:
Hi: I am not sure if this is the right place to post this question. Please let me know if it is not and I appreciate if someone could point me in the right direction. I am getting this error...
1
by: Optimus | last post by:
Hi everyone, I currently develop an application in vs.net 2005 with vb.net. I was trying to use typed dataset and I've got in trouble for converting untyped dataset into Typed DataSet. I don't...
0
by: hlyall1189 | last post by:
Hi, I recently started upgrading some of my old vs 2003 apps to vs 2005 and used the conversion tool but now i get the following error after building the page. I have typecasted the lines as...
3
by: keithb | last post by:
What could be causing this? this code: String Com = ""; if (Com != (String)rw.ItemArray) fails at runtime with the error message: Unable to cast object of type 'System.Int32' to type...
9
by: Jim in Arizona | last post by:
I get this error: Unable to cast object of type 'System.Web.UI.HtmlControls.HtmlInputText' to type 'System.Web.UI.WebControls.TextBox'. Using this code: Dim test3 As TextBox test3 =...
1
by: =?Utf-8?B?U2NvdHQ=?= | last post by:
Hello, Using VS2008 in a C# web service application, a class has been created that inherits from the ConfigurationSelection. This class file has been placed in the App_Code folder. The...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.