all groups > dotnet xml > february 2005 >
You're in the

dotnet xml

group:

Cannot implicitly convert type 'object' to 'System.Xml.XmlNode'



Cannot implicitly convert type 'object' to 'System.Xml.XmlNode' pieter_van_landuyt NO[at]SPAM hotmail.com
2/17/2005 5:58:40 AM
dotnet xml: Hi,
I have a problem with XmlNodes and my stack.
It looks something like this:

//declarations
XmlNode node, new_node;
Stack MyStack = new Stack();

//code
MyStack.Push(node);
new_node=MyStack.Pop();

The last line delivered an error like this:
Cannot implicitly convert type 'object' to 'System.Xml.XmlNode'

Could somebody please tell me what I'm doing wrong?

Re: Cannot implicitly convert type 'object' to 'System.Xml.XmlNode' Martin Honnen
2/17/2005 5:08:07 PM


[quoted text, click to view]


[quoted text, click to view]

You need to cast what the stack gives you e.g.
new_node = (XmlNode)MyStack.Pop();

In .NET 2.0 there will be generics and type safe collections but in .NET
1.x the stack can store all kind of different objects so if you get an
object from the stack you need to cast it to the type you want. Of
course that means in general if you put different kind of objects in
your stack that a cast can fail so use exception handling to cover that.

--

Martin Honnen
AddThis Social Bookmark Button