Odd Dictionary Error

Hey guys,

I’ve got a Dictionary that in one place in the app, I am storing string values that are obtained from a RegEx.

  Dim d As New Xojo.Core.Dictionary
 
  d.Value("HRES") = match.SubExpressionString(1)
  d.Value("VRES") = match.SubExpressionString(2)
  d.Value("FrameRate") = match.SubExpressionString(3) 
  d.Value("Format") = match.SubExpressionString(4)
  d.Value("AspectRatio") = match.SubExpressionString(5)
  d.Value("ColorDepth") = match.SubExpressionString(6)

I then store that dictionary as a property of the object.

Later, in another spot in my code, I do the following:

Dim s as String = d.Value("HRES")+"x"+d.Value("VRES")
DeviceList.Cell(row,5) = s

I get a type mismatch exception - “Expected Text but got String.”

I’ve tried changing the code to:

Dim t as Text = d.Value("HRES")+"x"+d.Value("VRES")
Dim s as string = t
DeviceList.Cell(row,5) = s

But I still get the same error. If I put string into a dictionary, I should get string back out since it uses Autos to store the values…

Xojo.Core.Dictionary stores Text, not String.

I can’t reproduce this. I used this as test code.

dim s as string = "hi"

dim d as new Xojo.Core.Dictionary
d.Value( "a" ) = s
dim a as Auto = d.Value( "a" )

dim ti as Xojo.Introspection.TypeInfo = Xojo.Introspection.GetType( a )
MsgBox ti.Name

I get String.

I’m using all Xojo.Core.Dictionary though. So it should be OK.

If I do this:

Dim hres As String = d.Value("HRES")
Dim vres As String = d.Value("VRES")
Dim s as string = hres+"x"+vres

It works just fine. But somehow when using the expression to pull the values from the dictionary, Xojo is expecting everything to be Text. That seems like a Bug.

No, it stores Auto. So if you store a String, it should return a string. And anyhow, if it stored Text, then this should have worked:

Dim t as Text = d.Value("HRES")+"x"+d.Value("VRES")

But it didn’t.

I know I had an issue a year(?) ago with using string literals with Xojo.Core.Dictionary but I believe it was fixed rather quickly. What version of Xojo are you using?

[quote=322135:@Kem Tekinay]I can’t reproduce this. I used this as test code.

dim s as string = "hi"

dim d as new Xojo.Core.Dictionary
d.Value( "a" ) = s
dim a as Auto = d.Value( "a" )

dim ti as Xojo.Introspection.TypeInfo = Xojo.Introspection.GetType( a )
MsgBox ti.Name

I get String.[/quote]

Kem,

Try this:

Sub Open()
Dim s1 As String = "Hi"
Dim s2 As String = "There"

Dim d As New Xojo.Core.Dictionary

d.value("a") = s1
d.value("b") = s2

Dim s3 As String = d.value("a")+" "+d.value("b")

MsgBox s3
End Sub

It’s the expression of concatenating the two dictionary values where it gets hosed.
Now, I’m assuming that since a dictionary stores Autos and since Autos are supposed to give back the same type you put in, that this concatenation should work - but it doesn’t.

This causes the problem for me

[code] Dim rg As New RegEx
Dim match As RegExMatch
dim sval as string = “1 2 3 4 5 6 7”

	rg.SearchPattern="(\\d+) (\\d+) (\\d+) (\\d+) (\\d+) (\\d+) (\\d+)"
	match=rg.Search(sval)
	if match<>nil then
			Dim d As New Xojo.Core.Dictionary
			 
			  d.Value("HRES") = match.SubExpressionString(1)
			  d.Value("VRES") = match.SubExpressionString(2)
			  d.Value("FrameRate") = match.SubExpressionString(3) 
			  d.Value("Format") = match.SubExpressionString(4)
			  d.Value("AspectRatio") = match.SubExpressionString(5)
			  d.Value("ColorDepth") = match.SubExpressionString(6)
			
			Dim s as String = d.Value("HRES")+"x"+d.Value("VRES")
			break
	end if[/code]

Lee

OK, I got it.

When you included the literal text “x” in the expression, the compiler must get confused. Try this:

Dim s as String = d.Value("HRES")+ Ctype( "x", String ) +d.Value("VRES")

or, even better:

const kX as string = "x"
Dim s as String = d.Value("HRES")+ kX +d.Value("VRES")

This is OK.

Dim hres as String = d.Value("HRES") dim vres as string = d.Value("VRES") dim s as string = hres+"x"+vres

[quote=322143:@Kem Tekinay]OK, I got it.

When you included the literal text “x” in the expression, the compiler must get confused. Try this:

Dim s as String = d.Value("HRES")+ Ctype( "x", String ) +d.Value("VRES")

or, even better:

const kX as string = "x" Dim s as String = d.Value("HRES")+ kX +d.Value("VRES") [/quote]

Why would I assume that “x” is a text literal? If I do:

Dim s1 as String = "Hi"
Dim s2 as String = "There"

Dim s3 as String = S1+" "+S2

That works.

It looks like the literal (“x”) is confusing the expression.

You can see the same thing here:

Dim s1 As String = "test" Dim a As Auto = s1 Dim s2 As String = a + "x"

[quote=322147:@Paul Lefebvre]It looks like the literal (“x”) is confusing the expression.

You can see the same thing here:

Dim s1 As String = "test" Dim a As Auto = s1 Dim s2 As String = a + "x"[/quote]

It is but that seems like a bug in the framework which is due to this tremendously confusing overlay of strings and text. I understand why you guy developed the Text type but it’s a mess.

Dim s1 as string = "bo"
Dim s2 as string = "top"
Dim s3 as string = s1+"x"+s2

Will work fine. “x” here is seen as a string value.

Since Auto is supposed to give back what you put in, why is the framework expecting it to be text?

I’m filing a bug report for this…

OK. The plot thickens! I discovered that this does not work:

Sub Open()
Dim s1 As String = "Hi "
Dim s2 As String = "There"

Dim d As New Xojo.Core.Dictionary

d.value("a") = s1
d.value("b") = s2

Dim s3 As String = d.value("a")+d.value("b")

MsgBox s3
End Sub

Apparently Auto does not have the “+” operator. So the framework is assuming that +" "+ here is text not string. It’s the “+” operator that is screwing things up. Xojo does not check to see what is stored in the auto before doing operations on it. You have to extract it first or perhaps use Ctype.

Auto makes far fewer assumptions than Variant. This is inconvenient in some respects but also safer. You have to be explicit about what you want.

A literal can be either a String or Text depending on context and your usage is making the context unclear, I guess. But if you’re using String anyway, can you also use the faster classic Dictionary where you can specify Value().StringValue?

That is sensitive to encoding issues unlike the new dictionary
Pick yer poison

In this code the second addition of “abc” works in the old dictionary simply because the encoding is different
In the new dictionary it doesn’t add "abc"again and compares string keys with different encodings correctly

  dim OldDict as new dictionary
  dim NewDict as new xojo.Core.Dictionary
		
  dim s as string
  s = "abc"
  s = ConvertEncoding(s, Encodings.ASCII)
  if OldDict.HasKey(s) = false then 
    OldDict.value(s) = s
  end if
  if NewDict.HasKey(s) = false then 
    NewDict.value(s) = s
  end if
		
  s = "abc"
  s = ConvertEncoding(s, Encodings.UTF16)
  if OldDict.HasKey(s) = false then 
    OldDict.value(s) = s
  end if
  if NewDict.HasKey(s) = false then 
    NewDict.value(s) = s
  end if
		
  break