Expecting String got Variant

Dim linklistinfo() As String = Array(n.value("label"), n.value("url"), n.value("keyword"), n.value("timestamp"), n.value("clicks"))

When I try to compile right now, I am getting:

Type Mismatch Error. Expected String() Got Variant()

Using this in another project as well… and it works fine. I can not for the life of me figure out why it thinks this is a variant?

Because the Dictionary.Value function returns a Variant, so you are creating an array of Variant. Try this instead:

... = Array(n.Value("label").StringValue, n.Value("url").StringValue, ...)

That works… not sure why I didn’t have to do that in the last project… but it works :wink:

Thanks Kem!

I can’t imagine why. Array will attempt to build an array that best matches its parameters. Given a mix or where each element is a Variant, it will build a Variant array.

Getting json back and using jsonitem and looping through it. So are you saying if it sees integer values, or dates, it will try to cast them as something else on it’s own? Or for instance that CLICK value, is a number in the database, that the json is generated from, it will try and interpret that as an integer value? Because the variable holding the json at first is a String.

Example of json:

{"label":"fdfgfdsdf","url":"http:\\/\\/7662376273.com","keyword":"7xbrmtlhayefe7xjjtkwow","timestamp":"2016-12-21 13:29:18","clicks":"0"}

Sorry I am a PHP guy, and this Xojo is pretty new, so just trying to grasp why it happened. :wink:

Or… are you saying Dictionary.Value does that based on what it thinks the value of that item is?

I’m saying the Array operator, specifically, will attempt to determine the type of array it should generate based on its parameters.

Examples:

Array( 1, 2, 3 ) --> Integer()
Array( "a", "b", "c" ) --> String()
Array( 1.0, 2.0, 3.0 ) --> Double()
Array( true, false ) --> Boolean()
Array( true, 1.0, "hi" ) --> Variant()

So did the issue arise when he tried to put the variant array into a String variable then?

Apparently… since I set it to string. Was just trying to determine why it didn’t happen in another project I used it in, wrapping my head around it. :wink:

[quote=304945:@Kem Tekinay]I’m saying the Array operator, specifically, will attempt to determine the type of array it should generate based on its parameters.

Examples:

Array( 1, 2, 3 ) --> Integer() Array ( "a", "b", "c" ) --> String() Array ( 1.0, 2.0, 3.0 ) --> Double() Array ( true, false ) --> Boolean() Array ( true, 1.0, "hi" ) --> Variant() [/quote]

Thanks again for explaining it in more detail. That is actually what I figured you were saying to begin with. Just doesn’t make sense where I am also using it in another project, since it is very similar. But, I get it now, and appreciate the answers. :wink:

BTW, while Array tries to guess at the type of array to create, it’s very easy to create helper functions that are specific. For example:

Function StringArray (ParamArray values() As String) As String()
  return values
End Function

Function VariantArray (ParamArray values() As Variant) As Variant()
  return values()
End Function

And so on.