469,610 Members | 1,554 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,610 developers. It's quick & easy.

Problem with Typecast in Generic class "Cannot convert type 'string' to 'T' "

Hi All,
I am facing the problem in Typecast inside the Generic class.
I need a function which will take two param 1:-Xpath 2:- XMLDOC
and return me what i will need.

here is the code for accessing to the Generic class.

Expand|Select|Wrap|Line Numbers
  1. private double mtrxgraphdata;
  2. private XMLdocument Matrixdata;
  3.  
  4. GraphControl.XMLUtility<double> obj = new XMLUtility<double>();
  5.  mtrxgraphdata = obj.GetElement("//Matrix/matrixdata/",Matrixdata);
  6.  
  7.  
  8.  
  9. Here is the code for the Generic class.
  10. class XMLUtility<T>
  11.           {
  12.             public T GetElement(string Xpath,XmlDocument XmlDoc)
  13.             {
  14.  
  15.                 XmlNodeList Elements = XmlDoc.SelectNodes(Xpath);
  16.                 T[] returnList = new T[Elements.Count];
  17.                     int counter = 0;
  18.                     foreach (XmlNode node in Elements)
  19.                     {
  20.                         returnList[counter] =(T) node.InnerText;// here i am getting  error  "Cannot convert type 'string' to 'T' "
  21.                         counter++;
  22.                     }
  23.                 }
  24.                 return returnList;
  25.  
  26.             }
  27.          }
Thanks in advance
Jan 25 '08 #1
1 7964
rt7777
1
try this:

http://goingspare.wordpress.com/2006/10/22/generic-methods-now-theres-a-thing/

especially: attValue = (T)Convert.ChangeType(strVal, typeof(T));
Feb 21 '08 #2

Post your reply

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

Similar topics

16 posts views Thread by ondekoza | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.