This code works until an Entry.Value contains a number and then throws a type mismatch error, as it should.
processData(data as xojo.core.Dictionary)
dim theString as String = ""
For Each entry As DictionaryEntry In data
//do something here with each entry of the passed data
dim myEntryKey as Auto = entry.Key
dim myEntryValue as Auto = entry.Value
msgbox(myEntryKey + ":" + myEntryValue) //<-----type mismatch thrown here
Next
This code also works until an Entry.Value contains a number but throws the type mismatch earlier when I try to force it to string
processData(data as xojo.core.Dictionary)
dim theString as String = ""
For Each entry As DictionaryEntry In data
//do something here with each entry of the passed data
dim myEntryKey as Auto = entry.Key
dim myEntryValue as String = entry.Value //<-----type mismatch thrown here
msgbox(myEntryKey + ":" + myEntryValue)
Next
This code does not build. Throws the error “There is more than one item with this name and it’s not clear to which this refers.”
processData(data as xojo.core.Dictionary)
dim theString as String = ""
For Each entry As DictionaryEntry In data
//do something here with each entry of the passed data
dim myEntryKey as Auto = entry.Key
dim myEntryValue as String = entry.Value //
msgbox(myEntryKey + ":" + myEntryValue.toText)<-----build error
Next
This code also does not build. Throws the same error as above.
processData(data as xojo.core.Dictionary)
dim theString as String = ""
For Each entry As DictionaryEntry In data
//do something here with each entry of the passed data
dim myEntryKey as Auto = entry.Key
dim myEntryValue as String = entry.Value //
msgbox(myEntryKey + ":" + str(myEntryValue))<-----build error
Next
What’s going on here? What’s the difference between Text and String? Some methods require strings, others require text and most of time I’m just trying to move data around. The MsgBox is a good example.
Since I can’t edit this Post directly–
THE LAST TWO EXAMPLES SHOULD SAY:
This code does not build. Throws the error “There is more than one item with this name and it’s not clear to which this refers.”
processData(data as xojo.core.Dictionary)
dim theString as String = ""
For Each entry As DictionaryEntry In data
//do something here with each entry of the passed data
dim myEntryKey as Auto = entry.Key
dim myEntryValue as Auto = entry.Value //
msgbox(myEntryKey + ":" + myEntryValue.toText)<-----build error
Next
This code also does not build. Throws the same error as above.
processData(data as xojo.core.Dictionary)
dim theString as String = ""
For Each entry As DictionaryEntry In data
//do something here with each entry of the passed data
dim myEntryKey as Auto = entry.Key
dim myEntryValue as Auto = entry.Value //
msgbox(myEntryKey + ":" + str(myEntryValue))<-----build error
Next
What's going on here? What's the difference between Text and String? Some methods require strings, others require text and most of time I'm just trying to move data around. The MsgBox is a good example.
Auto does not type convert. You cannot assign an Auto variable that contains a number to a String. you first have to assign it to its original type and then convert it using available type functions:
Dim i As Integer = entry.Value // Dict value contains an integer
Dim t As Text = i.ToText // Convert Integer to Text
Dim s As String = i.ToText // Text automatically converts to String
You last two examples build fine for me (for desktop), although it doesn’t make sense to call Str on a String as you do in #4.
If the type in the Auto variable can change, then you can check its type using Introspection. There is an example on the Auto doc page.
I was trying to edit the example while you were posting a reply, as it had an error in it. They were supposed to have said:
dim myEntryValue as Auto
Yes, the problem is that I don’t know what the entry.Value is going to be in advance, so I can’t assign a specific type to it. Checking the type using introspection is the way I solved the issue. Seems a bit messy and convoluted… and boolean fails to convert to text so I’ll have to find a workaround for that. I really want to convert ALL Entry.Value’s to Text as they are from Xojo.Data.ParseJSON. I don’t have control over how the JSON is formed on the sending end (yet), so I have to take what I’m given and try to clean and conform it myself before sending it on.
For Each entry As DictionaryEntry In data
//do something here with each entry of the passed data
dim myEntryKey as Auto = entry.Key
dim info As Xojo.Introspection.TypeInfo = Xojo.Introspection.GetType(entry.Value)
dim convertedEntryValue as Text
Select Case info.Name
Case "Int64"
convertedEntryValue = CType(entry.Value, Int64).ToText + info.Name
Case "Int32"
convertedEntryValue = CType(entry.Value, Int32).ToText + info.Name
Case "Int16"
convertedEntryValue = CType(entry.Value, Int16).ToText + info.Name
Case "Int8"
convertedEntryValue = CType(entry.Value, Int8).ToText + info.Name
Case "Integer"
convertedEntryValue = CType(entry.Value, Integer).ToText + info.Name
Case "Double"
convertedEntryValue = CType(entry.Value, Double).ToText + info.Name
Case "Boolean"
//convertedEntryValue = CType(entry.Value, Boolean).ToText
Else
convertedEntryValue = entry.Value + info.Name
End Select
System.DebugLog(myEntryKey + ":" + convertedEntryValue)
Next