# API1, API2 & Xojo.Core Question

**URL:** <https://ifnotnil.com/t/api1-api2-xojo-core-question/831>\
**Category:** General Programming\
**Created:** [8 August 2020 22:40 UTC](https://ifnotnil.com/t/api1-api2-xojo-core-question/831 "2020-08-08T22:40:42Z")\
**Posts on this page:** 13\
**Page:** 3

<div class="post-metadata">

**Author:** ![anic297](https://ifnotnil.com/user_avatar/ifnotnil.com/anic297/32/18_2.png) [@anic297](https://ifnotnil.com/u/anic297)\
**Post date:** [10 August 2020 16:22 UTC](https://ifnotnil.com/t/api1-api2-xojo-core-question/831/42 "2020-08-10T16:22:59Z")

</div>

> [@Garry](#):
>
> I have only used API 2.0 since it was released and only use it on current projects and I find it absolutely fine to work with.

I wish I could take the habit of using API2, for the long run, but since API1 is still supported, I usually write as I know (API1 syntax) and don’t learn API2 much.  
If only there was a switch to deactivate API1, to learn API2…

---

<div class="post-metadata">

**Author:** ![anic297](https://ifnotnil.com/user_avatar/ifnotnil.com/anic297/32/18_2.png) [@anic297](https://ifnotnil.com/u/anic297)\
**Post date:** [10 August 2020 16:29 UTC](https://ifnotnil.com/t/api1-api2-xojo-core-question/831/43 "2020-08-10T16:29:37Z")

</div>

> [@npalardy](#):
>
> Currently that second use case requires you to add a string variable or constant that you can then use in that expression and THAT is a potential source of new bugs

While I haven’t tried, I’d give an attempt to this:  
if String(“0123456789”).InStr(key)  
Which wouldn’t require variable or constant. I’d expect this to work…

---

<div class="post-metadata">

**Author:** ![DaveS](https://ifnotnil.com/user_avatar/ifnotnil.com/daves/32/79_2.png) [@DaveS](https://ifnotnil.com/u/DaveS)\
**Post date:** [10 August 2020 16:40 UTC](https://ifnotnil.com/t/api1-api2-xojo-core-question/831/44 "2020-08-10T16:40:43Z")

</div>

well yeah it still does require a constant… it is just you are casting it (unnecessarily) to a String… in this case Constant and Literal Value are the same thing

---

<div class="post-metadata">

**Author:** ![DaveS](https://ifnotnil.com/user_avatar/ifnotnil.com/daves/32/79_2.png) [@DaveS](https://ifnotnil.com/u/DaveS)\
**Post date:** [10 August 2020 16:42 UTC](https://ifnotnil.com/t/api1-api2-xojo-core-question/831/45 "2020-08-10T16:42:53Z")

</div>

```auto
dim s as string = "12345678"
const c = "12345678"
if s.instr(key) then ..... // variable
if c.instr(key) then .... // constant
if "12345678".instr(key) then ... // literal
if String("12345678").instr(key) then .... // still a literal

```

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [10 August 2020 16:43 UTC](https://ifnotnil.com/t/api1-api2-xojo-core-question/831/46 "2020-08-10T16:43:57Z")

</div>

> [@anic297](#):
>
> `if String(“0123456789”).InStr(key)`  
> Which wouldn’t require variable or constant. I’d expect this to work…

heh 🙂  
you need more contortions than that to make it work  
a cast wont work  
this one gives you

```auto
Type mismatch error. Expected String, but got TextLiteral
If String("0123456789").InStr(key) > 0 Then

```

you end up needing to do

```auto
If CType("0123456789",String).InStr(key) > 0 Then

```

and this is also true in most places a literal might be used

---

<div class="post-metadata">

**Author:** ![anic297](https://ifnotnil.com/user_avatar/ifnotnil.com/anic297/32/18_2.png) [@anic297](https://ifnotnil.com/u/anic297)\
**Post date:** [10 August 2020 17:17 UTC](https://ifnotnil.com/t/api1-api2-xojo-core-question/831/47 "2020-08-10T17:17:48Z")

</div>

> [@DaveS](#):
>
> well yeah it still does require a constant… it is just you are casting it (unnecessarily) to a String… in this case Constant and Literal Value are the same thing

Yes. I actually meant you avoid having “something” that holds the value (a dimmed variable or a constant defined).  
Casting, in this case, wouldn’t be unnecessary, as it’s how avoiding a “holder” would work. It just enforces the fact that it’s a string, so then the dotation way works.

---

<div class="post-metadata">

**Author:** ![ivan](https://ifnotnil.com/letter_avatar_proxy/v4/letter/i/df788c/32.png) [@ivan](https://ifnotnil.com/u/ivan)\
**Post date:** [10 August 2020 17:21 UTC](https://ifnotnil.com/t/api1-api2-xojo-core-question/831/48 "2020-08-10T17:21:46Z")

</div>

> [@anic297](#):
>
> I wish I could take the habit of using API2, for the long run, but since API1 is still supported, I usually write as I know (API1 syntax) and don’t learn API2 much.  
> If only there was a switch to deactivate API1, to learn API2…

Yes, it was one of my requests, Some kind of STRICT mode to allow API2 only mode, but, they prefer the mess of just warnings.

---

<div class="post-metadata">

**Author:** ![anic297](https://ifnotnil.com/user_avatar/ifnotnil.com/anic297/32/18_2.png) [@anic297](https://ifnotnil.com/u/anic297)\
**Post date:** [10 August 2020 17:23 UTC](https://ifnotnil.com/t/api1-api2-xojo-core-question/831/49 "2020-08-10T17:23:53Z")

</div>

> [@npalardy](#):
>
> heh 🙂  
> you need more contortions than that to make it work

I admit I never encountered this case (since API1 didn’t enforce this notation). I was also unaware of the CType function.  
So it’s a matter of choosing to hold the string in a variable or use a “convoluted” way.  
Now that I know about CType, I’d go for it if I ever come across this issue. Having a variable just to be explicit about the fact it’s a string (and allow methods on it) seems a waste of resources to me.

Thanks for pointing CType.

---

<div class="post-metadata">

**Author:** ![anic297](https://ifnotnil.com/user_avatar/ifnotnil.com/anic297/32/18_2.png) [@anic297](https://ifnotnil.com/u/anic297)\
**Post date:** [10 August 2020 17:25 UTC](https://ifnotnil.com/t/api1-api2-xojo-core-question/831/50 "2020-08-10T17:25:58Z")

</div>

> [@ivan](#):
>
> Yes, it was one of my requests, Some kind of STRICT mode to allow API2 only mode, but, they prefer the mess of just warnings.

Well, I usually check for warnings after substantial changes; I don’t think I would notice I was using API1 before I’d have to rewrite a lot (thus avoiding API2 all the way).

---

<div class="post-metadata">

**Author:** ![Emile](https://ifnotnil.com/letter_avatar_proxy/v4/letter/e/58956e/32.png) [@Emile](https://ifnotnil.com/u/Emile)\
**Post date:** [10 August 2020 18:33 UTC](https://ifnotnil.com/t/api1-api2-xojo-core-question/831/51 "2020-08-10T18:33:26Z")

</div>

Yes Norman.

But with iOS noted everywhere, that is how I was feeling things.  
Also, Desktop extension (New Framework for Desktop) does not appears. So, I stayed with my wrong, understanding.

This was only a perception issue.

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [10 August 2020 18:52 UTC](https://ifnotnil.com/t/api1-api2-xojo-core-question/831/52 "2020-08-10T18:52:08Z")

</div>

> [@anic297](#):
>
> So it’s a matter of choosing to hold the string in a variable or use a “convoluted” way.  
> Now that I know about CType, I’d go for it if I ever come across this issue. Having a variable just to be explicit about the fact it’s a string (and allow methods on it) seems a waste of resources to me.
> 
> Thanks for pointing CType.

The problem with having to use a variable is that a literal is more like a constant

I could declare a local constant and do

```auto
const localNumbersConstant = "0123456789"

If localNumbersConstant.InStr(key) > 0 Then
   ...
End if

```

Again there’s a new “variable” of some kind ot have to go refer to when you look at that line of code

It’s all trade offs

---

<div class="post-metadata">

**Author:** ![PatrickSalo](https://ifnotnil.com/user_avatar/ifnotnil.com/patricksalo/32/670_2.png) [@PatrickSalo](https://ifnotnil.com/u/PatrickSalo)\
**Post date:** [11 August 2020 01:01 UTC](https://ifnotnil.com/t/api1-api2-xojo-core-question/831/53 "2020-08-11T01:01:03Z")

</div>

> [@npalardy](#):
>
> heh 🙂  
> you need more contortions than that to make it work  
> a cast wont work  
> this one gives you
> 
> ```auto
> Type mismatch error. Expected String, but got TextLiteral
> If String("0123456789").InStr(key) > 0 Then
> 
> ```
> 
> you end up needing to do
> 
> ```auto
> If CType("0123456789",String).InStr(key) > 0 Then
> 
> ```
> 
> and this is also true in most places a literal might be used

This is one of my biggest pet peeves and I’m thrilled to learn about CType here. @npalardy do you know why a normal cast won’t work and does anyone know if Xojo is going to be make this easier/more intuitive down the road?

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [11 August 2020 01:10 UTC](https://ifnotnil.com/t/api1-api2-xojo-core-question/831/54 "2020-08-11T01:10:09Z")

</div>

> [@PatrickSalo](#):
>
> @npalardy do you know why a normal cast won’t work

To be kind of simplistic about this a Cast doesnt alter the TYPE that same way Ctype does  
Its more like a simple hint rather than a type conversion  
Its all kind of hand wavy and really internal to how the compiler treats things  
Suffice to say that CType DOES an explicit conversion and gives a “string” that can be used that way

> [@PatrickSalo](#):
>
> and does anyone know if Xojo is going to be make this easier/more intuitive down the road?

they have not said anything about this and, on the case I filed, their replies dont seem to indicate they understand why this might be needed or useful case # 56629 which, despite at least 2 requests, remains private so only testers can see it

My simple example was

> [@](#):
>
> In API 1.0 you could write
> 
> ```auto
> dim s as string = Right("123", 1)
> 
> ```
> 
> BUT in API 2.0 you cannot write something equivalent like
> 
> ```auto
> var s as string = "123".Right(1)
> 
> ```
> 
> The code is entirely frivolous but serves to illustrate the point of the request  
> There are places where we will ALWAY have to revert to the API 1.0 forms for use with literals - right left mid val and so many other because they cannot be used with a literal

which, while trivial and frivolous, illustrates that there are things API 1 could do that API 2 cannot without adding variables constants or cluttering the code with CTYPE’s

[Previous page](https://ifnotnil.com/t/api1-api2-xojo-core-question/831.md?page=2)
