Syntax Error on XmlDocument.Child(0)

Hello

I have a Getter that should return a XmlNode. This is my code:

Get Return New XmlDocument(AssetsIndexFile).Child(0) End Get

So in my mind: I create a new XmlDocument, the constructor of XmlDocument accepts a FolderItem (AssetsIndexFile is a FolderItem), then return the Child(0) of that XmlDocument, which is a XmlNode.

But obviously, I’m making a mistake, but I cannot see what is wrong here. Can anyone point me in the right direction?

Thanks!

Do it in steps

[quote]dim xdoc as XmlDocument
xdoc = New XmlDocument(AssetsIndexFile)
return xdoc.firstchild[/quote]

Hmh. That works indeed. Thanks!

Can any of the Xojo ellaborate on why my code doesn’t work? Now my code is 3 times longer which affects readability and maybe even performance.

Most languages support what I tried to do and there is no mention in the Return docs of this restriction.

‘affects readability’ … I agree. The 3 line source code is much more readable. :wink:

You could probably get it down to two:

dim xdoc as XmlDocument = New XmlDocument(AssetsIndexFile) return xdoc.firstchild

I suspect that the compiler is getting confused by the New XMLDocument but the requirement to return an XMLNode.

[quote=315055:@Jeff Tullin]‘affects readability’ … I agree. The 3 line source code is much more readable. :wink:
You could probably get it down to two:

dim xdoc as XmlDocument = New XmlDocument(AssetsIndexFile) return xdoc.firstchild
[/quote]

That’s highly subjective ofcourse, IMO, the one line code is the easiest to read, then the 3 lines, and 2 lines at last.

If you want to do it in 2 lines, why not:

dim xdoc as New XmlDocument(AssetsIndexFile) return xdoc.firstchild

Again, IMO that makes much more sense than the 2 lines you wrote. It’s only a small difference in coding, but the thinking behind it is more consistent. Make a new instance of type x. Instead of make a new instance of type x and assign that to object of type x.

Could be, but shouldn’t be, as XmlDocument is a subclass of XmlNode.
But again, this subject is really subjective and everyone has it’s own ‘best’ way to do things.

The only thing that really bothers me is the fact that this doesn’t compile, as there is nothing inherently wrong with it.

What is inherently wrong is that this is not valid Xojo syntax. You cannot use New in an expression like that.

To expand, “New” can’t be used as the left hand side of the “.” operator.