# Text type as Dictionary key

**URL:** <https://forum.xojo.com/t/text-type-as-dictionary-key/26437>\
**Category:** General\
**Created:** [September 3, 2015, 2:38pm UTC](https://forum.xojo.com/t/text-type-as-dictionary-key/26437 "2015-09-03T14:38:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian\_Max](https://forum.xojo.com/letter_avatar_proxy/v4/letter/b/e56c9b/32.png) [@Brian\_Max](https://forum.xojo.com/u/Brian_Max)\
**Post date:** [September 3, 2015, 2:38pm UTC](https://forum.xojo.com/t/text-type-as-dictionary-key/26437/1 "2015-09-03T14:38:38Z")

</div>

This works:

```auto
  Dim s As String = "StringValue"
  Dim d As New Dictionary
  d.Value(s) = "Dictionary Value"
  
  MsgBox(d.Value("StringValue"))
```

But this does not:

```auto
  Dim t As Text = "TextValue"
  Dim d As New Dictionary
  d.Value(t) = "Dictionary Value"
  
  MsgBox(d.Value("TextValue"))
```

I get KeyNotFoundException. Now my question is WHY and what’s the correct way for using Text variables with this example?

---

<div class="post-metadata">

**Author:** ![Eli\_Ott](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@Eli\_Ott](https://forum.xojo.com/u/Eli_Ott)\
**Post date:** [September 3, 2015, 2:44pm UTC](https://forum.xojo.com/t/text-type-as-dictionary-key/26437/2 "2015-09-03T14:44:51Z")

</div>

[code]Dim t As Text = “TextValue”  
Dim d As New Dictionary  
d.Value(t) = “Dictionary Value”

MsgBox(d.Value(“TextValue”)) \< “TextValue” is a String, not a Text[/code]

So either prepend the code with

```auto
Using Xojo.Core
```

or do this conversion:

```auto
MsgBox(d.Value(CType("TextValue", Text)))
```

---

<div class="post-metadata">

**Author:** ![Brian\_Max](https://forum.xojo.com/letter_avatar_proxy/v4/letter/b/e56c9b/32.png) [@Brian\_Max](https://forum.xojo.com/u/Brian_Max)\
**Post date:** [September 3, 2015, 2:51pm UTC](https://forum.xojo.com/t/text-type-as-dictionary-key/26437/3 "2015-09-03T14:51:13Z")

</div>

Always thought Text and String types are interchangeable. Oh well…

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [September 3, 2015, 3:25pm UTC](https://forum.xojo.com/t/text-type-as-dictionary-key/26437/4 "2015-09-03T15:25:03Z")

</div>

> [@211317:@Brian Max](#):
>
> Always thought Text and String types are interchangeable. Oh well…

Text has an implicit conversion to String, but not the other way. That is why you have to use .toText for a string to be affected to a Text variable.

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 5:55pm UTC](https://forum.xojo.com/t/text-type-as-dictionary-key/26437/5 "2020-10-29T17:55:53Z")

</div>


