Xojo.Core.DictionaryEntry with text, string & textliteral in a Desktop project

I have this block of code that iterates through a xojo.core.dictionary and creates xmldocument nodes.

Public Sub Export(xDoc As XmlDocument, xRoot As XmlNode) For Each Item As xojo.Core.DictionaryEntry In Data xRoot.AppendChild(xDoc.CreateElement(Item.Key)) If Item.Key = "PrivateKey" Then ' **** xRoot.LastChild.AppendChild(xDoc.CreateTextNode(xojo.Core.TextEncoding.UTF8.ConvertDataToText(Item.Value).ToCString(Xojo.Core.TextEncoding.UTF8))) Else xRoot.LastChild.AppendChild(xDoc.CreateTextNode(Item.Value)) End If Next End Sub

This code compiles ok, but at runtime fails on the line If Item.Key = "PrivateKey" Then ' **** with a type mismatch exception - expected Text but got String. So I tried

Public Sub Export(xDoc As XmlDocument, xRoot As XmlNode) For Each Item As xojo.Core.DictionaryEntry In Data xRoot.AppendChild(xDoc.CreateElement(Item.Key)) If Item.Key = Text("PrivateKey") Then ' **** xRoot.LastChild.AppendChild(xDoc.CreateTextNode(xojo.Core.TextEncoding.UTF8.ConvertDataToText(Item.Value).ToCString(Xojo.Core.TextEncoding.UTF8))) Else xRoot.LastChild.AppendChild(xDoc.CreateTextNode(Item.Value)) End If Next End Sub

Which won’t compile with a type mismatch error Expected Text, but got TextLiteral. So another try

Public Sub Export(xDoc As XmlDocument, xRoot As XmlNode) Dim t As Text = "PrivateKey" For Each Item As xojo.Core.DictionaryEntry In Data xRoot.AppendChild(xDoc.CreateElement(Item.Key)) If Item.Key = t Then ' **** xRoot.LastChild.AppendChild(xDoc.CreateTextNode(xojo.Core.TextEncoding.UTF8.ConvertDataToText(Item.Value).ToCString(Xojo.Core.TextEncoding.UTF8))) Else xRoot.LastChild.AppendChild(xDoc.CreateTextNode(Item.Value)) End If Next End Sub

Which fails at runtime with type mismatch error expected text but got string, but wait t is a text and that’s what the debug inspector says too. So having lost the plot on logic at this point I try this code

Public Sub Export(xDoc As XmlDocument, xRoot As XmlNode) Dim t As String = "PrivateKey" For Each Item As xojo.Core.DictionaryEntry In Data xRoot.AppendChild(xDoc.CreateElement(Item.Key)) If Item.Key = t Then ' **** xRoot.LastChild.AppendChild(xDoc.CreateTextNode(xojo.Core.TextEncoding.UTF8.ConvertDataToText(Item.Value).ToCString(Xojo.Core.TextEncoding.UTF8))) Else xRoot.LastChild.AppendChild(xDoc.CreateTextNode(Item.Value)) End If Next End Sub

And this works.

So now can someone please explain, so that I may learn, why the new framework dictionary entry which according to the documentation has a property “Key” as an auto will only accept a non-literal String but keeps on bleating about wanting a text type?

[quote=304373:@Wayne Golding]I have this block of code that iterates through a xojo.core.dictionary and creates xmldocument nodes.

Public Sub Export(xDoc As XmlDocument, xRoot As XmlNode) For Each Item As xojo.Core.DictionaryEntry In Data xRoot.AppendChild(xDoc.CreateElement(Item.Key)) If Item.Key = "PrivateKey" Then ' **** xRoot.LastChild.AppendChild(xDoc.CreateTextNode(xojo.Core.TextEncoding.UTF8.ConvertDataToText(Item.Value).ToCString(Xojo.Core.TextEncoding.UTF8))) Else xRoot.LastChild.AppendChild(xDoc.CreateTextNode(Item.Value)) End If Next End Sub

This code compiles ok, but at runtime fails on the line If Item.Key = "PrivateKey" Then ' **** with a type mismatch exception - expected Text but got String. So I tried[/quote]

This is either a bug or design flaw.

[quote=304373:@Wayne Golding]

Public Sub Export(xDoc As XmlDocument, xRoot As XmlNode) For Each Item As xojo.Core.DictionaryEntry In Data xRoot.AppendChild(xDoc.CreateElement(Item.Key)) If Item.Key = Text("PrivateKey") Then ' **** xRoot.LastChild.AppendChild(xDoc.CreateTextNode(xojo.Core.TextEncoding.UTF8.ConvertDataToText(Item.Value).ToCString(Xojo.Core.TextEncoding.UTF8))) Else xRoot.LastChild.AppendChild(xDoc.CreateTextNode(Item.Value)) End If Next End Sub

Which won’t compile with a type mismatch error Expected Text, but got TextLiteral. So another try[/quote]

This should probably have a better error reported, but CType is meant to be used in these situations.

CType was what I was looking for. Thanks Joe